| 1 | use {Sequence, Value}; |
| 2 | |
| 3 | /// A source of randomness. |
| 4 | pub trait Source { |
| 5 | /// Read `u64` uniformly distributed over `{0, 1, …, u64::MAX}`. |
| 6 | fn read_u64(&mut self) -> u64; |
| 7 | |
| 8 | /// Read `f64` uniformly distributed over `[0, 1]`. |
| 9 | #[inline(always)] |
| 10 | fn read_f64(&mut self) -> f64 { |
| 11 | self.read_u64() as f64 / ::std::u64::MAX as f64 |
| 12 | } |
| 13 | |
| 14 | /// Read a random value. |
| 15 | #[inline(always)] |
| 16 | fn read<V>(&mut self) -> V where Self: Sized, V: Value { |
| 17 | Value::read(self) |
| 18 | } |
| 19 | |
| 20 | /// Read a sequence of random values. |
| 21 | #[inline(always)] |
| 22 | fn iter<'l, V>(&'l mut self) -> Sequence<'l, Self, V> where Self: Sized, V: Value { |
| 23 | From::from(self) |
| 24 | } |
| 25 | } |