| ... | ... | @@ -69,22 +69,36 @@ pub const Random = struct { |
| 69 | 69 | pub fn uintLessThan(r: *Random, comptime T: type, less_than: T) T { |
| 70 | 70 | assert(T.is_signed == false); |
| 71 | 71 | assert(0 < less_than); |
| 72 | | |
| 73 | | const last_group_size_minus_one: T = maxInt(T) % less_than; |
| 74 | | if (last_group_size_minus_one == less_than - 1) { |
| 75 | | // less_than is a power of two. |
| 76 | | assert(math.floorPowerOfTwo(T, less_than) == less_than); |
| 77 | | // There is no retry zone. The optimal retry_zone_start would be maxInt(T) + 1. |
| 78 | | return r.int(T) % less_than; |
| 79 | | } |
| 80 | | const retry_zone_start = maxInt(T) - last_group_size_minus_one; |
| 81 | | |
| 82 | | while (true) { |
| 83 | | const rand_val = r.int(T); |
| 84 | | if (rand_val < retry_zone_start) { |
| 85 | | return rand_val % less_than; |
| 72 | // Small is typically u32 |
| 73 | const Small = @IntType(false, @divTrunc(T.bit_count + 31, 32) * 32); |
| 74 | // Large is typically u64 |
| 75 | const Large = @IntType(false, Small.bit_count * 2); |
| 76 | |
| 77 | // adapted from: |
| 78 | // http://www.pcg-random.org/posts/bounded-rands.html |
| 79 | // "Lemire's (with an extra tweak from me)" |
| 80 | var x: Small = r.int(Small); |
| 81 | var m: Large = Large(x) * Large(less_than); |
| 82 | var l: Small = @truncate(Small, m); |
| 83 | if (l < less_than) { |
| 84 | // TODO: workaround for https://github.com/ziglang/zig/issues/1770 |
| 85 | // should be: |
| 86 | // var t: Small = -%less_than; |
| 87 | var t: Small = @bitCast(Small, -%@bitCast(@IntType(true, Small.bit_count), Small(less_than))); |
| 88 | |
| 89 | if (t >= less_than) { |
| 90 | t -= less_than; |
| 91 | if (t >= less_than) { |
| 92 | t %= less_than; |
| 93 | } |
| 94 | } |
| 95 | while (l < t) { |
| 96 | x = r.int(Small); |
| 97 | m = Large(x) * Large(less_than); |
| 98 | l = @truncate(Small, m); |
| 86 | 99 | } |
| 87 | 100 | } |
| 101 | return @intCast(T, m >> Small.bit_count); |
| 88 | 102 | } |
| 89 | 103 | |
| 90 | 104 | /// Returns an evenly distributed random unsigned integer `0 <= i <= at_most`. |
| ... | ... | @@ -294,10 +308,19 @@ fn testRandomIntLessThan() void { |
| 294 | 308 | var r = SequentialPrng.init(); |
| 295 | 309 | r.next_value = 0xff; |
| 296 | 310 | assert(r.random.uintLessThan(u8, 4) == 3); |
| 297 | | r.next_value = 0xff; |
| 298 | | assert(r.random.uintLessThan(u8, 3) == 0); |
| 311 | assert(r.next_value == 0); |
| 312 | assert(r.random.uintLessThan(u8, 4) == 0); |
| 299 | 313 | assert(r.next_value == 1); |
| 300 | 314 | |
| 315 | r.next_value = 0; |
| 316 | assert(r.random.uintLessThan(u64, 32) == 0); |
| 317 | |
| 318 | // trigger the bias rejection code path |
| 319 | r.next_value = 0; |
| 320 | assert(r.random.uintLessThan(u8, 3) == 0); |
| 321 | // verify we incremented twice |
| 322 | assert(r.next_value == 2); |
| 323 | |
| 301 | 324 | r.next_value = 0xff; |
| 302 | 325 | assert(r.random.intRangeLessThan(u8, 0, 0x80) == 0x7f); |
| 303 | 326 | r.next_value = 0xff; |
| ... | ... | @@ -310,17 +333,10 @@ fn testRandomIntLessThan() void { |
| 310 | 333 | r.next_value = 0xff; |
| 311 | 334 | assert(r.random.intRangeLessThan(i8, -0x80, 0) == -1); |
| 312 | 335 | |
| 313 | | r.next_value = 0xff; |
| 314 | | assert(r.random.intRangeLessThan(i64, -0x8000000000000000, 0) == -1); |
| 315 | 336 | r.next_value = 0xff; |
| 316 | 337 | assert(r.random.intRangeLessThan(i3, -4, 0) == -1); |
| 317 | 338 | r.next_value = 0xff; |
| 318 | 339 | assert(r.random.intRangeLessThan(i3, -2, 2) == 1); |
| 319 | | |
| 320 | | // test retrying and eventually getting a good value |
| 321 | | // start just out of bounds |
| 322 | | r.next_value = 0x81; |
| 323 | | assert(r.random.uintLessThan(u8, 0x81) == 0); |
| 324 | 340 | } |
| 325 | 341 | |
| 326 | 342 | test "Random intAtMost" { |
| ... | ... | @@ -332,9 +348,14 @@ fn testRandomIntAtMost() void { |
| 332 | 348 | var r = SequentialPrng.init(); |
| 333 | 349 | r.next_value = 0xff; |
| 334 | 350 | assert(r.random.uintAtMost(u8, 3) == 3); |
| 335 | | r.next_value = 0xff; |
| 351 | assert(r.next_value == 0); |
| 352 | assert(r.random.uintAtMost(u8, 3) == 0); |
| 353 | |
| 354 | // trigger the bias rejection code path |
| 355 | r.next_value = 0; |
| 336 | 356 | assert(r.random.uintAtMost(u8, 2) == 0); |
| 337 | | assert(r.next_value == 1); |
| 357 | // verify we incremented twice |
| 358 | assert(r.next_value == 2); |
| 338 | 359 | |
| 339 | 360 | r.next_value = 0xff; |
| 340 | 361 | assert(r.random.intRangeAtMost(u8, 0, 0x7f) == 0x7f); |
| ... | ... | @@ -348,17 +369,10 @@ fn testRandomIntAtMost() void { |
| 348 | 369 | r.next_value = 0xff; |
| 349 | 370 | assert(r.random.intRangeAtMost(i8, -0x80, -1) == -1); |
| 350 | 371 | |
| 351 | | r.next_value = 0xff; |
| 352 | | assert(r.random.intRangeAtMost(i64, -0x8000000000000000, -1) == -1); |
| 353 | 372 | r.next_value = 0xff; |
| 354 | 373 | assert(r.random.intRangeAtMost(i3, -4, -1) == -1); |
| 355 | 374 | r.next_value = 0xff; |
| 356 | 375 | assert(r.random.intRangeAtMost(i3, -2, 1) == 1); |
| 357 | | |
| 358 | | // test retrying and eventually getting a good value |
| 359 | | // start just out of bounds |
| 360 | | r.next_value = 0x81; |
| 361 | | assert(r.random.uintAtMost(u8, 0x80) == 0); |
| 362 | 376 | } |
| 363 | 377 | |
| 364 | 378 | // Generator to extend 64-bit seed values into longer sequences. |