ombre: Update `rng` module and its docs
884cec5e7949b22a3e63c0b7e691b7bc199403ec
1 parent
5d3bc9f4
ombre/examples/particles.rs
+3 -3
| 97 | 97 | for _ in 0..PARTICLES_PER_FRAME { |
|
| 98 | 98 | if self.particles.len() >= MAX_PARTICLES { |
|
| 99 | 99 | break; |
|
| 100 | 100 | } |
|
| 101 | 101 | // Generate random angle and radius for circular distribution. |
|
| 102 | - | let angle = rng.probability() as f32 * std::f32::consts::PI * 2.0; |
|
| 102 | + | let angle = rng.fraction() as f32 * std::f32::consts::PI * 2.0; |
|
| 103 | 103 | let spread = 1.4f32; |
|
| 104 | - | let radius = rng.probability() as f32 * spread; |
|
| 104 | + | let radius = rng.fraction() as f32 * spread; |
|
| 105 | 105 | ||
| 106 | 106 | // Convert polar coordinates to cartesian. |
|
| 107 | 107 | let vx = radius * angle.cos(); |
|
| 108 | 108 | let vz = radius * angle.sin(); |
|
| 109 | 109 | ||
| 110 | 110 | self.particles.push(Particle { |
|
| 111 | 111 | pos: Vector3D::new(0.0, 0.0, 0.0), |
|
| 112 | 112 | vel: Vector3D::new( |
|
| 113 | 113 | vx, |
|
| 114 | - | rng.probability() as f32 * 2.0 + 1.5, // Upward velocity. |
|
| 114 | + | rng.fraction() as f32 * 2.0 + 1.5, // Upward velocity. |
|
| 115 | 115 | vz, |
|
| 116 | 116 | ), |
|
| 117 | 117 | age: 0.0, |
|
| 118 | 118 | }); |
|
| 119 | 119 | } |
ombre/src/rng.rs
+13 -4
| 50 | 50 | Self::new() |
|
| 51 | 51 | } |
|
| 52 | 52 | } |
|
| 53 | 53 | ||
| 54 | 54 | impl Rng { |
|
| 55 | + | /// Creates a new RNG with a random seed derived from system entropy. |
|
| 55 | 56 | #[inline] |
|
| 56 | 57 | pub fn new() -> Self { |
|
| 57 | 58 | let mut bytes = [0u8; 8]; |
|
| 58 | 59 | let seed = if random(&mut bytes) { |
|
| 59 | 60 | u64::from_ne_bytes(bytes) |
| 65 | 66 | hasher.finish() |
|
| 66 | 67 | }; |
|
| 67 | 68 | Self::with_seed(seed) |
|
| 68 | 69 | } |
|
| 69 | 70 | ||
| 71 | + | /// Samples a value from the given distribution. |
|
| 70 | 72 | pub fn sample<T>(&mut self, dist: &impl Distribution<T>) -> T { |
|
| 71 | 73 | dist.sample(self) |
|
| 72 | 74 | } |
|
| 73 | 75 | ||
| 76 | + | /// Creates a new RNG with the specified seed for deterministic generation. |
|
| 74 | 77 | #[inline] |
|
| 75 | 78 | pub fn with_seed(seed: u64) -> Self { |
|
| 76 | 79 | Self { state: seed } |
|
| 77 | 80 | } |
|
| 78 | 81 | ||
| 79 | - | pub fn probability(&mut self) -> f64 { |
|
| 82 | + | /// Generates a random float in the range `[0.0, 1.0)` with 53 bits of precision. |
|
| 83 | + | pub fn fraction(&mut self) -> f64 { |
|
| 80 | 84 | const PRECISION: u32 = f64::MANTISSA_DIGITS + 1; |
|
| 81 | 85 | const MANTISSA_SCALE: f64 = 1.0 / ((1u64 << PRECISION) as f64); |
|
| 82 | 86 | ||
| 83 | 87 | let mut r = self.gen_u64(); |
|
| 84 | 88 | ||
| 85 | 89 | r >>= 64 - PRECISION; |
|
| 86 | 90 | r as f64 * MANTISSA_SCALE |
|
| 87 | 91 | } |
|
| 88 | 92 | ||
| 93 | + | /// Generates a random value of type `T` within the given range. |
|
| 94 | + | /// Panics if the range is empty. |
|
| 89 | 95 | #[inline] |
|
| 90 | 96 | pub fn gen<T, R>(&mut self, range: R) -> T |
|
| 91 | 97 | where |
|
| 92 | 98 | T: Copy + PartialOrd + Sub<Output = T> + Add<Output = T> + Bounded + One + fmt::Debug, |
|
| 93 | 99 | R: RangeBounds<T>, |
|
| 94 | 100 | Uniform<T>: Distribution<T>, |
|
| 95 | 101 | { |
|
| 96 | 102 | Uniform::from_range(range).sample(self) |
|
| 97 | 103 | } |
|
| 98 | 104 | ||
| 105 | + | /// Generates a random boolean with 50/50 probability. |
|
| 99 | 106 | #[inline] |
|
| 100 | 107 | pub fn flip(&mut self) -> bool { |
|
| 101 | 108 | self.gen_u32() & 1 == 1 |
|
| 102 | 109 | } |
|
| 103 | 110 |
| 111 | 118 | #[must_use] |
|
| 112 | 119 | pub fn fork(&mut self) -> Self { |
|
| 113 | 120 | Rng::with_seed(self.gen_u64()) |
|
| 114 | 121 | } |
|
| 115 | 122 | ||
| 123 | + | /// Randomly shuffles the elements of the slice in place using Fisher-Yates algorithm. |
|
| 116 | 124 | #[inline] |
|
| 117 | 125 | pub fn shuffle<T>(&mut self, slice: &mut [T]) { |
|
| 118 | 126 | for i in 1..slice.len() { |
|
| 119 | 127 | slice.swap(i, self.gen(..=i)); |
|
| 120 | 128 | } |
|
| 121 | 129 | } |
|
| 122 | 130 | ||
| 131 | + | /// Fills a byte slice with random values. |
|
| 123 | 132 | pub fn fill(&mut self, slice: &mut [u8]) { |
|
| 124 | 133 | // We fill the slice by chunks of 8 bytes, or one block of |
|
| 125 | 134 | // WyRand output per new state. |
|
| 126 | 135 | let mut chunks = slice.chunks_exact_mut(core::mem::size_of::<u64>()); |
|
| 127 | 136 | for chunk in chunks.by_ref() { |
| 138 | 147 | // Use the remaining length to copy from block |
|
| 139 | 148 | remainder.copy_from_slice(&n[..remainder.len()]); |
|
| 140 | 149 | } |
|
| 141 | 150 | } |
|
| 142 | 151 | ||
| 143 | - | /// Select a random element from a slice |
|
| 152 | + | /// Returns a random reference to an element from the slice, or None if empty. |
|
| 144 | 153 | #[inline] |
|
| 145 | 154 | pub fn choose<'a, T>(&mut self, slice: &'a [T]) -> Option<&'a T> { |
|
| 146 | 155 | if slice.is_empty() { |
|
| 147 | 156 | None |
|
| 148 | 157 | } else { |
|
| 149 | 158 | Some(&slice[self.gen(..slice.len())]) |
|
| 150 | 159 | } |
|
| 151 | 160 | } |
|
| 152 | 161 | ||
| 153 | - | /// Select a random mutable element from a slice |
|
| 162 | + | /// Returns a random mutable reference to an element from the slice, or None if empty. |
|
| 154 | 163 | #[inline] |
|
| 155 | 164 | pub fn choose_mut<'a, T>(&mut self, slice: &'a mut [T]) -> Option<&'a mut T> { |
|
| 156 | 165 | if slice.is_empty() { |
|
| 157 | 166 | None |
|
| 158 | 167 | } else { |
| 274 | 283 | fn distribution_f64(rng: &mut Rng, samples: usize, buckets: usize) { |
|
| 275 | 284 | let mut counts = vec![0; buckets]; |
|
| 276 | 285 | let bucket_size = 1.0 / buckets as f64; |
|
| 277 | 286 | ||
| 278 | 287 | for _ in 0..samples { |
|
| 279 | - | let value = rng.probability(); |
|
| 288 | + | let value = rng.fraction(); |
|
| 280 | 289 | let bucket = (value / bucket_size) as usize; |
|
| 281 | 290 | counts[bucket.min(buckets - 1)] += 1; |
|
| 282 | 291 | } |
|
| 283 | 292 | ||
| 284 | 293 | let expected = samples as f64 / buckets as f64; |