ombre: Fix RNG for `i32` ranges
a23e08b94aa7db738c227d0d7659d5bd3c583684
1 parent
b00015b6
ombre/src/math/traits.rs
+67 -29
| 252 | 252 | + Shl<u32, Output = Self> |
|
| 253 | 253 | { |
|
| 254 | 254 | /// Number of bits in this integer type. |
|
| 255 | 255 | const BITS: u32; |
|
| 256 | 256 | ||
| 257 | - | /// Calculates remainder when divided by other. |
|
| 258 | - | fn rem(&self, other: Self) -> Self; |
|
| 259 | - | /// Converts a u64 to this integer type. |
|
| 257 | + | /// Converts a [`u64`] to this integer type. |
|
| 260 | 258 | fn from_u64(other: u64) -> Self; |
|
| 261 | - | /// Converts this integer to f64. |
|
| 262 | - | fn as_f64(&self) -> f64; |
|
| 259 | + | /// Converts this integer to [`u64`]. |
|
| 260 | + | fn as_u64(&self) -> u64; |
|
| 261 | + | /// Wrapping add. |
|
| 262 | + | fn wrapping_add(self, rhs: Self) -> Self; |
|
| 263 | + | /// Wrapping sub. |
|
| 264 | + | fn wrapping_sub(self, rhs: Self) -> Self; |
|
| 263 | 265 | } |
|
| 264 | 266 | ||
| 265 | 267 | impl Integer for u32 { |
|
| 266 | 268 | const BITS: u32 = u32::BITS; |
|
| 267 | 269 | ||
| 268 | - | fn rem(&self, other: Self) -> Self { |
|
| 269 | - | self % other |
|
| 270 | - | } |
|
| 271 | - | ||
| 272 | 270 | fn from_u64(other: u64) -> Self { |
|
| 273 | 271 | other as u32 |
|
| 274 | 272 | } |
|
| 275 | 273 | ||
| 276 | - | fn as_f64(&self) -> f64 { |
|
| 277 | - | *self as f64 |
|
| 274 | + | fn as_u64(&self) -> u64 { |
|
| 275 | + | *self as u64 |
|
| 276 | + | } |
|
| 277 | + | ||
| 278 | + | fn wrapping_add(self, rhs: Self) -> Self { |
|
| 279 | + | self.wrapping_add(rhs) |
|
| 280 | + | } |
|
| 281 | + | ||
| 282 | + | fn wrapping_sub(self, rhs: Self) -> Self { |
|
| 283 | + | self.wrapping_sub(rhs) |
|
| 278 | 284 | } |
|
| 279 | 285 | } |
|
| 280 | 286 | ||
| 281 | 287 | impl Integer for i32 { |
|
| 282 | 288 | const BITS: u32 = i32::BITS; |
|
| 283 | 289 | ||
| 284 | - | fn rem(&self, other: Self) -> Self { |
|
| 285 | - | self % other |
|
| 290 | + | fn from_u64(other: u64) -> Self { |
|
| 291 | + | (other % (i32::MAX as u64)) as i32 |
|
| 286 | 292 | } |
|
| 287 | 293 | ||
| 288 | - | fn from_u64(other: u64) -> Self { |
|
| 289 | - | other as i32 |
|
| 294 | + | fn as_u64(&self) -> u64 { |
|
| 295 | + | *self as u64 |
|
| 290 | 296 | } |
|
| 291 | 297 | ||
| 292 | - | fn as_f64(&self) -> f64 { |
|
| 293 | - | *self as f64 |
|
| 298 | + | fn wrapping_add(self, rhs: Self) -> Self { |
|
| 299 | + | self.wrapping_add(rhs) |
|
| 300 | + | } |
|
| 301 | + | ||
| 302 | + | fn wrapping_sub(self, rhs: Self) -> Self { |
|
| 303 | + | self.wrapping_sub(rhs) |
|
| 294 | 304 | } |
|
| 295 | 305 | } |
|
| 296 | 306 | ||
| 297 | 307 | impl Integer for u64 { |
|
| 298 | 308 | const BITS: u32 = u64::BITS; |
|
| 299 | 309 | ||
| 300 | - | fn rem(&self, other: Self) -> Self { |
|
| 301 | - | self % other |
|
| 302 | - | } |
|
| 303 | - | ||
| 304 | 310 | fn from_u64(other: u64) -> Self { |
|
| 305 | 311 | other |
|
| 306 | 312 | } |
|
| 307 | 313 | ||
| 308 | - | fn as_f64(&self) -> f64 { |
|
| 309 | - | *self as f64 |
|
| 314 | + | fn as_u64(&self) -> u64 { |
|
| 315 | + | *self |
|
| 316 | + | } |
|
| 317 | + | ||
| 318 | + | fn wrapping_add(self, rhs: Self) -> Self { |
|
| 319 | + | self.wrapping_add(rhs) |
|
| 320 | + | } |
|
| 321 | + | ||
| 322 | + | fn wrapping_sub(self, rhs: Self) -> Self { |
|
| 323 | + | self.wrapping_sub(rhs) |
|
| 310 | 324 | } |
|
| 311 | 325 | } |
|
| 312 | 326 | ||
| 313 | 327 | impl Integer for usize { |
|
| 314 | 328 | const BITS: u32 = usize::BITS; |
|
| 315 | 329 | ||
| 316 | - | fn rem(&self, other: Self) -> Self { |
|
| 317 | - | self % other |
|
| 318 | - | } |
|
| 319 | - | ||
| 320 | 330 | fn from_u64(other: u64) -> Self { |
|
| 321 | 331 | other as usize |
|
| 322 | 332 | } |
|
| 323 | 333 | ||
| 324 | - | fn as_f64(&self) -> f64 { |
|
| 325 | - | *self as f64 |
|
| 334 | + | fn as_u64(&self) -> u64 { |
|
| 335 | + | *self as u64 |
|
| 336 | + | } |
|
| 337 | + | ||
| 338 | + | fn wrapping_add(self, rhs: Self) -> Self { |
|
| 339 | + | self.wrapping_add(rhs) |
|
| 340 | + | } |
|
| 341 | + | ||
| 342 | + | fn wrapping_sub(self, rhs: Self) -> Self { |
|
| 343 | + | self.wrapping_sub(rhs) |
|
| 326 | 344 | } |
|
| 327 | 345 | } |
|
| 328 | 346 | ||
| 329 | 347 | //////////////////////////////////////////////////////////////////////////////// |
|
| 330 | 348 |
| 456 | 474 | } |
|
| 457 | 475 | } |
|
| 458 | 476 | ||
| 459 | 477 | #[cfg(test)] |
|
| 460 | 478 | mod test { |
|
| 479 | + | use super::*; |
|
| 461 | 480 | use crate::math::macros::*; |
|
| 462 | 481 | ||
| 463 | 482 | #[test] |
|
| 464 | 483 | fn test_approx_eq() { |
|
| 465 | 484 | assert_approx_eq!(1.0, 1.0, 0.0001); |
|
| 466 | 485 | assert_approx_eq!(1.0, 1.001, 0.01); |
|
| 467 | 486 | assert_approx_neq!(1.0, 1.001, 0.00000001); |
|
| 468 | 487 | } |
|
| 488 | + | ||
| 489 | + | #[test] |
|
| 490 | + | fn test_integer_wrapping() { |
|
| 491 | + | let max_u32 = u32::MAX; |
|
| 492 | + | assert_eq!(max_u32.wrapping_add(1), 0); |
|
| 493 | + | assert_eq!(0_u32.wrapping_sub(1), max_u32); |
|
| 494 | + | ||
| 495 | + | let max_i32 = i32::MAX; |
|
| 496 | + | assert_eq!(max_i32.wrapping_add(1), i32::MIN); |
|
| 497 | + | assert_eq!(i32::MIN.wrapping_sub(1), i32::MAX); |
|
| 498 | + | } |
|
| 499 | + | ||
| 500 | + | #[test] |
|
| 501 | + | fn test_integer_conversions() { |
|
| 502 | + | assert_eq!(42_u32.as_u64(), 42_u64); |
|
| 503 | + | assert_eq!(42_i32.as_u64(), 42_u64); |
|
| 504 | + | assert_eq!(42_u64.as_u64(), 42_u64); |
|
| 505 | + | assert_eq!(42_usize.as_u64(), 42_u64); |
|
| 506 | + | } |
|
| 469 | 507 | } |
ombre/src/rng.rs
+9 -4
| 254 | 254 | T: Copy + PartialOrd + Integer, |
|
| 255 | 255 | { |
|
| 256 | 256 | fn sample<R: Entropy>(&self, rng: &mut R) -> T { |
|
| 257 | 257 | let low = self.low; |
|
| 258 | 258 | let high = self.high; |
|
| 259 | - | let len = high - low; |
|
| 260 | 259 | ||
| 261 | 260 | if low == T::MIN && high == T::MAX { |
|
| 262 | 261 | T::from_u64(rng.gen_u64()) |
|
| 263 | 262 | } else { |
|
| 264 | - | low + T::from_u64(rng.gen_u64()).rem(len) |
|
| 263 | + | let len = high.wrapping_sub(low).wrapping_add(T::ONE); |
|
| 264 | + | low.wrapping_add(T::from_u64(rng.gen_u64() % len.as_u64())) |
|
| 265 | 265 | } |
|
| 266 | 266 | } |
|
| 267 | 267 | } |
|
| 268 | 268 | ||
| 269 | 269 | #[cfg(test)] |
| 277 | 277 | range: std::ops::Range<T>, |
|
| 278 | 278 | samples: usize, |
|
| 279 | 279 | buckets: usize, |
|
| 280 | 280 | ) { |
|
| 281 | 281 | let mut counts = vec![0; buckets]; |
|
| 282 | - | let bucket_size = (range.end - range.start).as_f64() / buckets as f64; |
|
| 282 | + | let bucket_size = (range.end - range.start).as_u64() as f64 / buckets as f64; |
|
| 283 | 283 | ||
| 284 | 284 | for _ in 0..samples { |
|
| 285 | 285 | let value = rng.gen(range.clone()); |
|
| 286 | - | let bucket = ((value - range.start).as_f64() / bucket_size) as usize; |
|
| 286 | + | let bucket = ((value - range.start).as_u64() as f64 / bucket_size) as usize; |
|
| 287 | 287 | counts[bucket.min(buckets - 1)] += 1; |
|
| 288 | 288 | } |
|
| 289 | 289 | ||
| 290 | 290 | let expected = samples as f64 / buckets as f64; |
|
| 291 | 291 | for (i, &count) in counts.iter().enumerate() { |
| 327 | 327 | let _: bool = rng.flip(); |
|
| 328 | 328 | ||
| 329 | 329 | // Using gen_range |
|
| 330 | 330 | let n = rng.gen(1u64..=100); |
|
| 331 | 331 | assert!((1..=100).contains(&n)); |
|
| 332 | + | ||
| 333 | + | for _ in 0..10 { |
|
| 334 | + | let n = rng.gen(0..60); |
|
| 335 | + | assert!((0..60).contains(&n), "{n}"); |
|
| 336 | + | } |
|
| 332 | 337 | } |
|
| 333 | 338 | ||
| 334 | 339 | #[test] |
|
| 335 | 340 | fn test_fill_bytes() { |
|
| 336 | 341 | let mut rng = Rng::new(); |