| author | |
| committer | |
| log | ed63d6c7fdbdc3c508457ca792436595dd443ac8 |
| tree | 1fc0bfeae8cbb443d7ab416b0308a0db1149ab8a |
| parent | e0a514df41cdf40b0e99ba48b86143bfc03dd3aa |
| parent | 7bedeb9659af84daab33a4804c54b4c8811c0c5d |
| signature |
Improve stdlib's random float generation2 files changed, 489 insertions(+), 326 deletions(-)
lib/std/rand.zig+42-326| ... | @@ -9,8 +9,6 @@ | ... | @@ -9,8 +9,6 @@ |
| 9 | const std = @import("std.zig"); | 9 | const std = @import("std.zig"); |
| 10 | const builtin = @import("builtin"); | 10 | const builtin = @import("builtin"); |
| 11 | const assert = std.debug.assert; | 11 | const assert = std.debug.assert; |
| 12 | const expect = std.testing.expect; | ||
| 13 | const expectEqual = std.testing.expectEqual; | ||
| 14 | const mem = std.mem; | 12 | const mem = std.mem; |
| 15 | const math = std.math; | 13 | const math = std.math; |
| 16 | const ziggurat = @import("rand/ziggurat.zig"); | 14 | const ziggurat = @import("rand/ziggurat.zig"); |
| ... | @@ -249,18 +247,51 @@ pub const Random = struct { | ... | @@ -249,18 +247,51 @@ pub const Random = struct { |
| 249 | 247 | ||
| 250 | /// Return a floating point value evenly distributed in the range [0, 1). | 248 | /// Return a floating point value evenly distributed in the range [0, 1). |
| 251 | pub fn float(r: Random, comptime T: type) T { | 249 | pub fn float(r: Random, comptime T: type) T { |
| 252 | // Generate a uniform value between [1, 2) and scale down to [0, 1). | 250 | // Generate a uniformly random value between for the mantissa. |
| 253 | // Note: The lowest mantissa bit is always set to 0 so we only use half the available range. | 251 | // Then generate an exponentially biased random value for the exponent. |
| 252 | // Over the previous method, this has the advantage of being able to | ||
| 253 | // represent every possible value in the available range. | ||
| 254 | switch (T) { | 254 | switch (T) { |
| 255 | f32 => { | 255 | f32 => { |
| 256 | const s = r.int(u32); | 256 | // Use 23 random bits for the mantissa, and the rest for the exponent. |
| 257 | const repr = (0x7f << 23) | (s >> 9); | 257 | // If all 41 bits are zero, generate additional random bits, until a |
| 258 | return @bitCast(f32, repr) - 1.0; | 258 | // set bit is found, or 126 bits have been generated. |
| 259 | const rand = r.int(u64); | ||
| 260 | var rand_lz = @clz(u64, rand | 0x7FFFFF); | ||
| 261 | if (rand_lz == 41) { | ||
| 262 | rand_lz += @clz(u64, r.int(u64)); | ||
| 263 | if (rand_lz == 41 + 64) { | ||
| 264 | // It is astronomically unlikely to reach this point. | ||
| 265 | rand_lz += @clz(u32, r.int(u32) | 0x7FF); | ||
| 266 | } | ||
| 267 | } | ||
| 268 | const mantissa = @truncate(u23, rand); | ||
| 269 | const exponent = @as(u32, 126 - rand_lz) << 23; | ||
| 270 | return @bitCast(f32, exponent | mantissa); | ||
| 259 | }, | 271 | }, |
| 260 | f64 => { | 272 | f64 => { |
| 261 | const s = r.int(u64); | 273 | // Use 52 random bits for the mantissa, and the rest for the exponent. |
| 262 | const repr = (0x3ff << 52) | (s >> 12); | 274 | // If all 12 bits are zero, generate additional random bits, until a |
| 263 | return @bitCast(f64, repr) - 1.0; | 275 | // set bit is found, or 1022 bits have been generated. |
| 276 | const rand = r.int(u64); | ||
| 277 | var rand_lz: u64 = @clz(u64, rand | 0xFFFFFFFFFFFFF); | ||
| 278 | if (rand_lz == 12) { | ||
| 279 | while (true) { | ||
| 280 | // It is astronomically unlikely for this loop to execute more than once. | ||
| 281 | const addl_rand_lz = @clz(u64, r.int(u64)); | ||
| 282 | rand_lz += addl_rand_lz; | ||
| 283 | if (addl_rand_lz != 64) { | ||
| 284 | break; | ||
| 285 | } | ||
| 286 | if (rand_lz >= 1022) { | ||
| 287 | rand_lz = 1022; | ||
| 288 | break; | ||
| 289 | } | ||
| 290 | } | ||
| 291 | } | ||
| 292 | const mantissa = rand & 0xFFFFFFFFFFFFF; | ||
| 293 | const exponent = (1022 - rand_lz) << 52; | ||
| 294 | return @bitCast(f64, exponent | mantissa); | ||
| 264 | }, | 295 | }, |
| 265 | else => @compileError("unknown floating point type"), | 296 | else => @compileError("unknown floating point type"), |
| 266 | } | 297 | } |
| ... | @@ -319,221 +350,6 @@ pub fn limitRangeBiased(comptime T: type, random_int: T, less_than: T) T { | ... | @@ -319,221 +350,6 @@ pub fn limitRangeBiased(comptime T: type, random_int: T, less_than: T) T { |
| 319 | return @intCast(T, m >> bits); | 350 | return @intCast(T, m >> bits); |
| 320 | } | 351 | } |
| 321 | 352 | ||
| 322 | const SequentialPrng = struct { | ||
| 323 | const Self = @This(); | ||
| 324 | next_value: u8, | ||
| 325 | |||
| 326 | pub fn init() Self { | ||
| 327 | return Self{ | ||
| 328 | .next_value = 0, | ||
| 329 | }; | ||
| 330 | } | ||
| 331 | |||
| 332 | pub fn random(self: *Self) Random { | ||
| 333 | return Random.init(self, fill); | ||
| 334 | } | ||
| 335 | |||
| 336 | pub fn fill(self: *Self, buf: []u8) void { | ||
| 337 | for (buf) |*b| { | ||
| 338 | b.* = self.next_value; | ||
| 339 | } | ||
| 340 | self.next_value +%= 1; | ||
| 341 | } | ||
| 342 | }; | ||
| 343 | |||
| 344 | test "Random int" { | ||
| 345 | try testRandomInt(); | ||
| 346 | comptime try testRandomInt(); | ||
| 347 | } | ||
| 348 | fn testRandomInt() !void { | ||
| 349 | var rng = SequentialPrng.init(); | ||
| 350 | const random = rng.random(); | ||
| 351 | |||
| 352 | try expect(random.int(u0) == 0); | ||
| 353 | |||
| 354 | rng.next_value = 0; | ||
| 355 | try expect(random.int(u1) == 0); | ||
| 356 | try expect(random.int(u1) == 1); | ||
| 357 | try expect(random.int(u2) == 2); | ||
| 358 | try expect(random.int(u2) == 3); | ||
| 359 | try expect(random.int(u2) == 0); | ||
| 360 | |||
| 361 | rng.next_value = 0xff; | ||
| 362 | try expect(random.int(u8) == 0xff); | ||
| 363 | rng.next_value = 0x11; | ||
| 364 | try expect(random.int(u8) == 0x11); | ||
| 365 | |||
| 366 | rng.next_value = 0xff; | ||
| 367 | try expect(random.int(u32) == 0xffffffff); | ||
| 368 | rng.next_value = 0x11; | ||
| 369 | try expect(random.int(u32) == 0x11111111); | ||
| 370 | |||
| 371 | rng.next_value = 0xff; | ||
| 372 | try expect(random.int(i32) == -1); | ||
| 373 | rng.next_value = 0x11; | ||
| 374 | try expect(random.int(i32) == 0x11111111); | ||
| 375 | |||
| 376 | rng.next_value = 0xff; | ||
| 377 | try expect(random.int(i8) == -1); | ||
| 378 | rng.next_value = 0x11; | ||
| 379 | try expect(random.int(i8) == 0x11); | ||
| 380 | |||
| 381 | rng.next_value = 0xff; | ||
| 382 | try expect(random.int(u33) == 0x1ffffffff); | ||
| 383 | rng.next_value = 0xff; | ||
| 384 | try expect(random.int(i1) == -1); | ||
| 385 | rng.next_value = 0xff; | ||
| 386 | try expect(random.int(i2) == -1); | ||
| 387 | rng.next_value = 0xff; | ||
| 388 | try expect(random.int(i33) == -1); | ||
| 389 | } | ||
| 390 | |||
| 391 | test "Random boolean" { | ||
| 392 | try testRandomBoolean(); | ||
| 393 | comptime try testRandomBoolean(); | ||
| 394 | } | ||
| 395 | fn testRandomBoolean() !void { | ||
| 396 | var rng = SequentialPrng.init(); | ||
| 397 | const random = rng.random(); | ||
| 398 | |||
| 399 | try expect(random.boolean() == false); | ||
| 400 | try expect(random.boolean() == true); | ||
| 401 | try expect(random.boolean() == false); | ||
| 402 | try expect(random.boolean() == true); | ||
| 403 | } | ||
| 404 | |||
| 405 | test "Random enum" { | ||
| 406 | try testRandomEnumValue(); | ||
| 407 | comptime try testRandomEnumValue(); | ||
| 408 | } | ||
| 409 | fn testRandomEnumValue() !void { | ||
| 410 | const TestEnum = enum { | ||
| 411 | First, | ||
| 412 | Second, | ||
| 413 | Third, | ||
| 414 | }; | ||
| 415 | var rng = SequentialPrng.init(); | ||
| 416 | const random = rng.random(); | ||
| 417 | rng.next_value = 0; | ||
| 418 | try expect(random.enumValue(TestEnum) == TestEnum.First); | ||
| 419 | try expect(random.enumValue(TestEnum) == TestEnum.First); | ||
| 420 | try expect(random.enumValue(TestEnum) == TestEnum.First); | ||
| 421 | } | ||
| 422 | |||
| 423 | test "Random intLessThan" { | ||
| 424 | @setEvalBranchQuota(10000); | ||
| 425 | try testRandomIntLessThan(); | ||
| 426 | comptime try testRandomIntLessThan(); | ||
| 427 | } | ||
| 428 | fn testRandomIntLessThan() !void { | ||
| 429 | var rng = SequentialPrng.init(); | ||
| 430 | const random = rng.random(); | ||
| 431 | |||
| 432 | rng.next_value = 0xff; | ||
| 433 | try expect(random.uintLessThan(u8, 4) == 3); | ||
| 434 | try expect(rng.next_value == 0); | ||
| 435 | try expect(random.uintLessThan(u8, 4) == 0); | ||
| 436 | try expect(rng.next_value == 1); | ||
| 437 | |||
| 438 | rng.next_value = 0; | ||
| 439 | try expect(random.uintLessThan(u64, 32) == 0); | ||
| 440 | |||
| 441 | // trigger the bias rejection code path | ||
| 442 | rng.next_value = 0; | ||
| 443 | try expect(random.uintLessThan(u8, 3) == 0); | ||
| 444 | // verify we incremented twice | ||
| 445 | try expect(rng.next_value == 2); | ||
| 446 | |||
| 447 | rng.next_value = 0xff; | ||
| 448 | try expect(random.intRangeLessThan(u8, 0, 0x80) == 0x7f); | ||
| 449 | rng.next_value = 0xff; | ||
| 450 | try expect(random.intRangeLessThan(u8, 0x7f, 0xff) == 0xfe); | ||
| 451 | |||
| 452 | rng.next_value = 0xff; | ||
| 453 | try expect(random.intRangeLessThan(i8, 0, 0x40) == 0x3f); | ||
| 454 | rng.next_value = 0xff; | ||
| 455 | try expect(random.intRangeLessThan(i8, -0x40, 0x40) == 0x3f); | ||
| 456 | rng.next_value = 0xff; | ||
| 457 | try expect(random.intRangeLessThan(i8, -0x80, 0) == -1); | ||
| 458 | |||
| 459 | rng.next_value = 0xff; | ||
| 460 | try expect(random.intRangeLessThan(i3, -4, 0) == -1); | ||
| 461 | rng.next_value = 0xff; | ||
| 462 | try expect(random.intRangeLessThan(i3, -2, 2) == 1); | ||
| 463 | } | ||
| 464 | |||
| 465 | test "Random intAtMost" { | ||
| 466 | @setEvalBranchQuota(10000); | ||
| 467 | try testRandomIntAtMost(); | ||
| 468 | comptime try testRandomIntAtMost(); | ||
| 469 | } | ||
| 470 | fn testRandomIntAtMost() !void { | ||
| 471 | var rng = SequentialPrng.init(); | ||
| 472 | const random = rng.random(); | ||
| 473 | |||
| 474 | rng.next_value = 0xff; | ||
| 475 | try expect(random.uintAtMost(u8, 3) == 3); | ||
| 476 | try expect(rng.next_value == 0); | ||
| 477 | try expect(random.uintAtMost(u8, 3) == 0); | ||
| 478 | |||
| 479 | // trigger the bias rejection code path | ||
| 480 | rng.next_value = 0; | ||
| 481 | try expect(random.uintAtMost(u8, 2) == 0); | ||
| 482 | // verify we incremented twice | ||
| 483 | try expect(rng.next_value == 2); | ||
| 484 | |||
| 485 | rng.next_value = 0xff; | ||
| 486 | try expect(random.intRangeAtMost(u8, 0, 0x7f) == 0x7f); | ||
| 487 | rng.next_value = 0xff; | ||
| 488 | try expect(random.intRangeAtMost(u8, 0x7f, 0xfe) == 0xfe); | ||
| 489 | |||
| 490 | rng.next_value = 0xff; | ||
| 491 | try expect(random.intRangeAtMost(i8, 0, 0x3f) == 0x3f); | ||
| 492 | rng.next_value = 0xff; | ||
| 493 | try expect(random.intRangeAtMost(i8, -0x40, 0x3f) == 0x3f); | ||
| 494 | rng.next_value = 0xff; | ||
| 495 | try expect(random.intRangeAtMost(i8, -0x80, -1) == -1); | ||
| 496 | |||
| 497 | rng.next_value = 0xff; | ||
| 498 | try expect(random.intRangeAtMost(i3, -4, -1) == -1); | ||
| 499 | rng.next_value = 0xff; | ||
| 500 | try expect(random.intRangeAtMost(i3, -2, 1) == 1); | ||
| 501 | |||
| 502 | try expect(random.uintAtMost(u0, 0) == 0); | ||
| 503 | } | ||
| 504 | |||
| 505 | test "Random Biased" { | ||
| 506 | var prng = DefaultPrng.init(0); | ||
| 507 | const random = prng.random(); | ||
| 508 | // Not thoroughly checking the logic here. | ||
| 509 | // Just want to execute all the paths with different types. | ||
| 510 | |||
| 511 | try expect(random.uintLessThanBiased(u1, 1) == 0); | ||
| 512 | try expect(random.uintLessThanBiased(u32, 10) < 10); | ||
| 513 | try expect(random.uintLessThanBiased(u64, 20) < 20); | ||
| 514 | |||
| 515 | try expect(random.uintAtMostBiased(u0, 0) == 0); | ||
| 516 | try expect(random.uintAtMostBiased(u1, 0) <= 0); | ||
| 517 | try expect(random.uintAtMostBiased(u32, 10) <= 10); | ||
| 518 | try expect(random.uintAtMostBiased(u64, 20) <= 20); | ||
| 519 | |||
| 520 | try expect(random.intRangeLessThanBiased(u1, 0, 1) == 0); | ||
| 521 | try expect(random.intRangeLessThanBiased(i1, -1, 0) == -1); | ||
| 522 | try expect(random.intRangeLessThanBiased(u32, 10, 20) >= 10); | ||
| 523 | try expect(random.intRangeLessThanBiased(i32, 10, 20) >= 10); | ||
| 524 | try expect(random.intRangeLessThanBiased(u64, 20, 40) >= 20); | ||
| 525 | try expect(random.intRangeLessThanBiased(i64, 20, 40) >= 20); | ||
| 526 | |||
| 527 | // uncomment for broken module error: | ||
| 528 | //expect(random.intRangeAtMostBiased(u0, 0, 0) == 0); | ||
| 529 | try expect(random.intRangeAtMostBiased(u1, 0, 1) >= 0); | ||
| 530 | try expect(random.intRangeAtMostBiased(i1, -1, 0) >= -1); | ||
| 531 | try expect(random.intRangeAtMostBiased(u32, 10, 20) >= 10); | ||
| 532 | try expect(random.intRangeAtMostBiased(i32, 10, 20) >= 10); | ||
| 533 | try expect(random.intRangeAtMostBiased(u64, 20, 40) >= 20); | ||
| 534 | try expect(random.intRangeAtMostBiased(i64, 20, 40) >= 20); | ||
| 535 | } | ||
| 536 | |||
| 537 | // Generator to extend 64-bit seed values into longer sequences. | 353 | // Generator to extend 64-bit seed values into longer sequences. |
| 538 | // | 354 | // |
| 539 | // The number of cycles is thus limited to 64-bits regardless of the engine, but this | 355 | // The number of cycles is thus limited to 64-bits regardless of the engine, but this |
| ... | @@ -555,107 +371,7 @@ pub const SplitMix64 = struct { | ... | @@ -555,107 +371,7 @@ pub const SplitMix64 = struct { |
| 555 | } | 371 | } |
| 556 | }; | 372 | }; |
| 557 | 373 | ||
| 558 | test "splitmix64 sequence" { | ||
| 559 | var r = SplitMix64.init(0xaeecf86f7878dd75); | ||
| 560 | |||
| 561 | const seq = [_]u64{ | ||
| 562 | 0x5dbd39db0178eb44, | ||
| 563 | 0xa9900fb66b397da3, | ||
| 564 | 0x5c1a28b1aeebcf5c, | ||
| 565 | 0x64a963238f776912, | ||
| 566 | 0xc6d4177b21d1c0ab, | ||
| 567 | 0xb2cbdbdb5ea35394, | ||
| 568 | }; | ||
| 569 | |||
| 570 | for (seq) |s| { | ||
| 571 | try expect(s == r.next()); | ||
| 572 | } | ||
| 573 | } | ||
| 574 | |||
| 575 | // Actual Random helper function tests, pcg engine is assumed correct. | ||
| 576 | test "Random float" { | ||
| 577 | var prng = DefaultPrng.init(0); | ||
| 578 | const random = prng.random(); | ||
| 579 | |||
| 580 | var i: usize = 0; | ||
| 581 | while (i < 1000) : (i += 1) { | ||
| 582 | const val1 = random.float(f32); | ||
| 583 | try expect(val1 >= 0.0); | ||
| 584 | try expect(val1 < 1.0); | ||
| 585 | |||
| 586 | const val2 = random.float(f64); | ||
| 587 | try expect(val2 >= 0.0); | ||
| 588 | try expect(val2 < 1.0); | ||
| 589 | } | ||
| 590 | } | ||
| 591 | |||
| 592 | test "Random shuffle" { | ||
| 593 | var prng = DefaultPrng.init(0); | ||
| 594 | const random = prng.random(); | ||
| 595 | |||
| 596 | var seq = [_]u8{ 0, 1, 2, 3, 4 }; | ||
| 597 | var seen = [_]bool{false} ** 5; | ||
| 598 | |||
| 599 | var i: usize = 0; | ||
| 600 | while (i < 1000) : (i += 1) { | ||
| 601 | random.shuffle(u8, seq[0..]); | ||
| 602 | seen[seq[0]] = true; | ||
| 603 | try expect(sumArray(seq[0..]) == 10); | ||
| 604 | } | ||
| 605 | |||
| 606 | // we should see every entry at the head at least once | ||
| 607 | for (seen) |e| { | ||
| 608 | try expect(e == true); | ||
| 609 | } | ||
| 610 | } | ||
| 611 | |||
| 612 | fn sumArray(s: []const u8) u32 { | ||
| 613 | var r: u32 = 0; | ||
| 614 | for (s) |e| | ||
| 615 | r += e; | ||
| 616 | return r; | ||
| 617 | } | ||
| 618 | |||
| 619 | test "Random range" { | ||
| 620 | var prng = DefaultPrng.init(0); | ||
| 621 | const random = prng.random(); | ||
| 622 | |||
| 623 | try testRange(random, -4, 3); | ||
| 624 | try testRange(random, -4, -1); | ||
| 625 | try testRange(random, 10, 14); | ||
| 626 | try testRange(random, -0x80, 0x7f); | ||
| 627 | } | ||
| 628 | |||
| 629 | fn testRange(r: Random, start: i8, end: i8) !void { | ||
| 630 | try testRangeBias(r, start, end, true); | ||
| 631 | try testRangeBias(r, start, end, false); | ||
| 632 | } | ||
| 633 | fn testRangeBias(r: Random, start: i8, end: i8, biased: bool) !void { | ||
| 634 | const count = @intCast(usize, @as(i32, end) - @as(i32, start)); | ||
| 635 | var values_buffer = [_]bool{false} ** 0x100; | ||
| 636 | const values = values_buffer[0..count]; | ||
| 637 | var i: usize = 0; | ||
| 638 | while (i < count) { | ||
| 639 | const value: i32 = if (biased) r.intRangeLessThanBiased(i8, start, end) else r.intRangeLessThan(i8, start, end); | ||
| 640 | const index = @intCast(usize, value - start); | ||
| 641 | if (!values[index]) { | ||
| 642 | i += 1; | ||
| 643 | values[index] = true; | ||
| 644 | } | ||
| 645 | } | ||
| 646 | } | ||
| 647 | |||
| 648 | test "CSPRNG" { | ||
| 649 | var secret_seed: [DefaultCsprng.secret_seed_length]u8 = undefined; | ||
| 650 | std.crypto.random.bytes(&secret_seed); | ||
| 651 | var csprng = DefaultCsprng.init(secret_seed); | ||
| 652 | const random = csprng.random(); | ||
| 653 | const a = random.int(u64); | ||
| 654 | const b = random.int(u64); | ||
| 655 | const c = random.int(u64); | ||
| 656 | try expect(a ^ b ^ c != 0); | ||
| 657 | } | ||
| 658 | |||
| 659 | test { | 374 | test { |
| 660 | std.testing.refAllDecls(@This()); | 375 | std.testing.refAllDecls(@This()); |
| 376 | _ = @import("rand/test.zig"); | ||
| 661 | } | 377 | } |
lib/std/rand/test.zig created+447| ... | @@ -0,0 +1,447 @@ | ||
| 1 | const std = @import("../std.zig"); | ||
| 2 | const math = std.math; | ||
| 3 | const DefaultPrng = std.rand.DefaultPrng; | ||
| 4 | const Random = std.rand.Random; | ||
| 5 | const SplitMix64 = std.rand.SplitMix64; | ||
| 6 | const DefaultCsprng = std.rand.DefaultCsprng; | ||
| 7 | const expect = std.testing.expect; | ||
| 8 | const expectEqual = std.testing.expectEqual; | ||
| 9 | |||
| 10 | const SequentialPrng = struct { | ||
| 11 | const Self = @This(); | ||
| 12 | next_value: u8, | ||
| 13 | |||
| 14 | pub fn init() Self { | ||
| 15 | return Self{ | ||
| 16 | .next_value = 0, | ||
| 17 | }; | ||
| 18 | } | ||
| 19 | |||
| 20 | pub fn random(self: *Self) Random { | ||
| 21 | return Random.init(self, fill); | ||
| 22 | } | ||
| 23 | |||
| 24 | pub fn fill(self: *Self, buf: []u8) void { | ||
| 25 | for (buf) |*b| { | ||
| 26 | b.* = self.next_value; | ||
| 27 | } | ||
| 28 | self.next_value +%= 1; | ||
| 29 | } | ||
| 30 | }; | ||
| 31 | |||
| 32 | /// Do not use this PRNG! It is meant to be predictable, for the purposes of test reproducibility and coverage. | ||
| 33 | /// Its output is just a repeat of a user-specified byte pattern. | ||
| 34 | /// Name is a reference to this comic: https://dilbert.com/strip/2001-10-25 | ||
| 35 | const Dilbert = struct { | ||
| 36 | pattern: []const u8 = undefined, | ||
| 37 | curr_idx: usize = 0, | ||
| 38 | |||
| 39 | pub fn init(pattern: []const u8) !Dilbert { | ||
| 40 | if (pattern.len == 0) | ||
| 41 | return error.EmptyPattern; | ||
| 42 | var self = Dilbert{}; | ||
| 43 | self.pattern = pattern; | ||
| 44 | self.curr_idx = 0; | ||
| 45 | return self; | ||
| 46 | } | ||
| 47 | |||
| 48 | pub fn random(self: *Dilbert) Random { | ||
| 49 | return Random.init(self, fill); | ||
| 50 | } | ||
| 51 | |||
| 52 | pub fn fill(self: *Dilbert, buf: []u8) void { | ||
| 53 | for (buf) |*byte| { | ||
| 54 | byte.* = self.pattern[self.curr_idx]; | ||
| 55 | self.curr_idx = (self.curr_idx + 1) % self.pattern.len; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | test "Dilbert fill" { | ||
| 60 | var r = try Dilbert.init("9nine"); | ||
| 61 | |||
| 62 | const seq = [_]u64{ | ||
| 63 | 0x396E696E65396E69, | ||
| 64 | 0x6E65396E696E6539, | ||
| 65 | 0x6E696E65396E696E, | ||
| 66 | 0x65396E696E65396E, | ||
| 67 | 0x696E65396E696E65, | ||
| 68 | }; | ||
| 69 | |||
| 70 | for (seq) |s| { | ||
| 71 | var buf0: [8]u8 = undefined; | ||
| 72 | var buf1: [8]u8 = undefined; | ||
| 73 | std.mem.writeIntBig(u64, &buf0, s); | ||
| 74 | r.fill(&buf1); | ||
| 75 | try std.testing.expect(std.mem.eql(u8, buf0[0..], buf1[0..])); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | }; | ||
| 79 | |||
| 80 | test "Random int" { | ||
| 81 | try testRandomInt(); | ||
| 82 | comptime try testRandomInt(); | ||
| 83 | } | ||
| 84 | fn testRandomInt() !void { | ||
| 85 | var rng = SequentialPrng.init(); | ||
| 86 | const random = rng.random(); | ||
| 87 | |||
| 88 | try expect(random.int(u0) == 0); | ||
| 89 | |||
| 90 | rng.next_value = 0; | ||
| 91 | try expect(random.int(u1) == 0); | ||
| 92 | try expect(random.int(u1) == 1); | ||
| 93 | try expect(random.int(u2) == 2); | ||
| 94 | try expect(random.int(u2) == 3); | ||
| 95 | try expect(random.int(u2) == 0); | ||
| 96 | |||
| 97 | rng.next_value = 0xff; | ||
| 98 | try expect(random.int(u8) == 0xff); | ||
| 99 | rng.next_value = 0x11; | ||
| 100 | try expect(random.int(u8) == 0x11); | ||
| 101 | |||
| 102 | rng.next_value = 0xff; | ||
| 103 | try expect(random.int(u32) == 0xffffffff); | ||
| 104 | rng.next_value = 0x11; | ||
| 105 | try expect(random.int(u32) == 0x11111111); | ||
| 106 | |||
| 107 | rng.next_value = 0xff; | ||
| 108 | try expect(random.int(i32) == -1); | ||
| 109 | rng.next_value = 0x11; | ||
| 110 | try expect(random.int(i32) == 0x11111111); | ||
| 111 | |||
| 112 | rng.next_value = 0xff; | ||
| 113 | try expect(random.int(i8) == -1); | ||
| 114 | rng.next_value = 0x11; | ||
| 115 | try expect(random.int(i8) == 0x11); | ||
| 116 | |||
| 117 | rng.next_value = 0xff; | ||
| 118 | try expect(random.int(u33) == 0x1ffffffff); | ||
| 119 | rng.next_value = 0xff; | ||
| 120 | try expect(random.int(i1) == -1); | ||
| 121 | rng.next_value = 0xff; | ||
| 122 | try expect(random.int(i2) == -1); | ||
| 123 | rng.next_value = 0xff; | ||
| 124 | try expect(random.int(i33) == -1); | ||
| 125 | } | ||
| 126 | |||
| 127 | test "Random boolean" { | ||
| 128 | try testRandomBoolean(); | ||
| 129 | comptime try testRandomBoolean(); | ||
| 130 | } | ||
| 131 | fn testRandomBoolean() !void { | ||
| 132 | var rng = SequentialPrng.init(); | ||
| 133 | const random = rng.random(); | ||
| 134 | |||
| 135 | try expect(random.boolean() == false); | ||
| 136 | try expect(random.boolean() == true); | ||
| 137 | try expect(random.boolean() == false); | ||
| 138 | try expect(random.boolean() == true); | ||
| 139 | } | ||
| 140 | |||
| 141 | test "Random enum" { | ||
| 142 | try testRandomEnumValue(); | ||
| 143 | comptime try testRandomEnumValue(); | ||
| 144 | } | ||
| 145 | fn testRandomEnumValue() !void { | ||
| 146 | const TestEnum = enum { | ||
| 147 | First, | ||
| 148 | Second, | ||
| 149 | Third, | ||
| 150 | }; | ||
| 151 | var rng = SequentialPrng.init(); | ||
| 152 | const random = rng.random(); | ||
| 153 | rng.next_value = 0; | ||
| 154 | try expect(random.enumValue(TestEnum) == TestEnum.First); | ||
| 155 | try expect(random.enumValue(TestEnum) == TestEnum.First); | ||
| 156 | try expect(random.enumValue(TestEnum) == TestEnum.First); | ||
| 157 | } | ||
| 158 | |||
| 159 | test "Random intLessThan" { | ||
| 160 | @setEvalBranchQuota(10000); | ||
| 161 | try testRandomIntLessThan(); | ||
| 162 | comptime try testRandomIntLessThan(); | ||
| 163 | } | ||
| 164 | fn testRandomIntLessThan() !void { | ||
| 165 | var rng = SequentialPrng.init(); | ||
| 166 | const random = rng.random(); | ||
| 167 | |||
| 168 | rng.next_value = 0xff; | ||
| 169 | try expect(random.uintLessThan(u8, 4) == 3); | ||
| 170 | try expect(rng.next_value == 0); | ||
| 171 | try expect(random.uintLessThan(u8, 4) == 0); | ||
| 172 | try expect(rng.next_value == 1); | ||
| 173 | |||
| 174 | rng.next_value = 0; | ||
| 175 | try expect(random.uintLessThan(u64, 32) == 0); | ||
| 176 | |||
| 177 | // trigger the bias rejection code path | ||
| 178 | rng.next_value = 0; | ||
| 179 | try expect(random.uintLessThan(u8, 3) == 0); | ||
| 180 | // verify we incremented twice | ||
| 181 | try expect(rng.next_value == 2); | ||
| 182 | |||
| 183 | rng.next_value = 0xff; | ||
| 184 | try expect(random.intRangeLessThan(u8, 0, 0x80) == 0x7f); | ||
| 185 | rng.next_value = 0xff; | ||
| 186 | try expect(random.intRangeLessThan(u8, 0x7f, 0xff) == 0xfe); | ||
| 187 | |||
| 188 | rng.next_value = 0xff; | ||
| 189 | try expect(random.intRangeLessThan(i8, 0, 0x40) == 0x3f); | ||
| 190 | rng.next_value = 0xff; | ||
| 191 | try expect(random.intRangeLessThan(i8, -0x40, 0x40) == 0x3f); | ||
| 192 | rng.next_value = 0xff; | ||
| 193 | try expect(random.intRangeLessThan(i8, -0x80, 0) == -1); | ||
| 194 | |||
| 195 | rng.next_value = 0xff; | ||
| 196 | try expect(random.intRangeLessThan(i3, -4, 0) == -1); | ||
| 197 | rng.next_value = 0xff; | ||
| 198 | try expect(random.intRangeLessThan(i3, -2, 2) == 1); | ||
| 199 | } | ||
| 200 | |||
| 201 | test "Random intAtMost" { | ||
| 202 | @setEvalBranchQuota(10000); | ||
| 203 | try testRandomIntAtMost(); | ||
| 204 | comptime try testRandomIntAtMost(); | ||
| 205 | } | ||
| 206 | fn testRandomIntAtMost() !void { | ||
| 207 | var rng = SequentialPrng.init(); | ||
| 208 | const random = rng.random(); | ||
| 209 | |||
| 210 | rng.next_value = 0xff; | ||
| 211 | try expect(random.uintAtMost(u8, 3) == 3); | ||
| 212 | try expect(rng.next_value == 0); | ||
| 213 | try expect(random.uintAtMost(u8, 3) == 0); | ||
| 214 | |||
| 215 | // trigger the bias rejection code path | ||
| 216 | rng.next_value = 0; | ||
| 217 | try expect(random.uintAtMost(u8, 2) == 0); | ||
| 218 | // verify we incremented twice | ||
| 219 | try expect(rng.next_value == 2); | ||
| 220 | |||
| 221 | rng.next_value = 0xff; | ||
| 222 | try expect(random.intRangeAtMost(u8, 0, 0x7f) == 0x7f); | ||
| 223 | rng.next_value = 0xff; | ||
| 224 | try expect(random.intRangeAtMost(u8, 0x7f, 0xfe) == 0xfe); | ||
| 225 | |||
| 226 | rng.next_value = 0xff; | ||
| 227 | try expect(random.intRangeAtMost(i8, 0, 0x3f) == 0x3f); | ||
| 228 | rng.next_value = 0xff; | ||
| 229 | try expect(random.intRangeAtMost(i8, -0x40, 0x3f) == 0x3f); | ||
| 230 | rng.next_value = 0xff; | ||
| 231 | try expect(random.intRangeAtMost(i8, -0x80, -1) == -1); | ||
| 232 | |||
| 233 | rng.next_value = 0xff; | ||
| 234 | try expect(random.intRangeAtMost(i3, -4, -1) == -1); | ||
| 235 | rng.next_value = 0xff; | ||
| 236 | try expect(random.intRangeAtMost(i3, -2, 1) == 1); | ||
| 237 | |||
| 238 | try expect(random.uintAtMost(u0, 0) == 0); | ||
| 239 | } | ||
| 240 | |||
| 241 | test "Random Biased" { | ||
| 242 | var prng = DefaultPrng.init(0); | ||
| 243 | const random = prng.random(); | ||
| 244 | // Not thoroughly checking the logic here. | ||
| 245 | // Just want to execute all the paths with different types. | ||
| 246 | |||
| 247 | try expect(random.uintLessThanBiased(u1, 1) == 0); | ||
| 248 | try expect(random.uintLessThanBiased(u32, 10) < 10); | ||
| 249 | try expect(random.uintLessThanBiased(u64, 20) < 20); | ||
| 250 | |||
| 251 | try expect(random.uintAtMostBiased(u0, 0) == 0); | ||
| 252 | try expect(random.uintAtMostBiased(u1, 0) <= 0); | ||
| 253 | try expect(random.uintAtMostBiased(u32, 10) <= 10); | ||
| 254 | try expect(random.uintAtMostBiased(u64, 20) <= 20); | ||
| 255 | |||
| 256 | try expect(random.intRangeLessThanBiased(u1, 0, 1) == 0); | ||
| 257 | try expect(random.intRangeLessThanBiased(i1, -1, 0) == -1); | ||
| 258 | try expect(random.intRangeLessThanBiased(u32, 10, 20) >= 10); | ||
| 259 | try expect(random.intRangeLessThanBiased(i32, 10, 20) >= 10); | ||
| 260 | try expect(random.intRangeLessThanBiased(u64, 20, 40) >= 20); | ||
| 261 | try expect(random.intRangeLessThanBiased(i64, 20, 40) >= 20); | ||
| 262 | |||
| 263 | // uncomment for broken module error: | ||
| 264 | //expect(random.intRangeAtMostBiased(u0, 0, 0) == 0); | ||
| 265 | try expect(random.intRangeAtMostBiased(u1, 0, 1) >= 0); | ||
| 266 | try expect(random.intRangeAtMostBiased(i1, -1, 0) >= -1); | ||
| 267 | try expect(random.intRangeAtMostBiased(u32, 10, 20) >= 10); | ||
| 268 | try expect(random.intRangeAtMostBiased(i32, 10, 20) >= 10); | ||
| 269 | try expect(random.intRangeAtMostBiased(u64, 20, 40) >= 20); | ||
| 270 | try expect(random.intRangeAtMostBiased(i64, 20, 40) >= 20); | ||
| 271 | } | ||
| 272 | |||
| 273 | test "splitmix64 sequence" { | ||
| 274 | var r = SplitMix64.init(0xaeecf86f7878dd75); | ||
| 275 | |||
| 276 | const seq = [_]u64{ | ||
| 277 | 0x5dbd39db0178eb44, | ||
| 278 | 0xa9900fb66b397da3, | ||
| 279 | 0x5c1a28b1aeebcf5c, | ||
| 280 | 0x64a963238f776912, | ||
| 281 | 0xc6d4177b21d1c0ab, | ||
| 282 | 0xb2cbdbdb5ea35394, | ||
| 283 | }; | ||
| 284 | |||
| 285 | for (seq) |s| { | ||
| 286 | try expect(s == r.next()); | ||
| 287 | } | ||
| 288 | } | ||
| 289 | |||
| 290 | // Actual Random helper function tests, pcg engine is assumed correct. | ||
| 291 | test "Random float correctness" { | ||
| 292 | var prng = DefaultPrng.init(0); | ||
| 293 | const random = prng.random(); | ||
| 294 | |||
| 295 | var i: usize = 0; | ||
| 296 | while (i < 1000) : (i += 1) { | ||
| 297 | const val1 = random.float(f32); | ||
| 298 | try expect(val1 >= 0.0); | ||
| 299 | try expect(val1 < 1.0); | ||
| 300 | |||
| 301 | const val2 = random.float(f64); | ||
| 302 | try expect(val2 >= 0.0); | ||
| 303 | try expect(val2 < 1.0); | ||
| 304 | } | ||
| 305 | } | ||
| 306 | |||
| 307 | // Check the "astronomically unlikely" code paths. | ||
| 308 | test "Random float coverage" { | ||
| 309 | var prng = try Dilbert.init(&[_]u8{0}); | ||
| 310 | const random = prng.random(); | ||
| 311 | |||
| 312 | const rand_f64 = random.float(f64); | ||
| 313 | const rand_f32 = random.float(f32); | ||
| 314 | |||
| 315 | try expect(rand_f32 == 0.0); | ||
| 316 | try expect(rand_f64 == 0.0); | ||
| 317 | } | ||
| 318 | |||
| 319 | test "Random float chi-square goodness of fit" { | ||
| 320 | const num_numbers = 100000; | ||
| 321 | const num_buckets = 1000; | ||
| 322 | |||
| 323 | var f32_hist = std.AutoHashMap(u32, u32).init(std.testing.allocator); | ||
| 324 | defer f32_hist.deinit(); | ||
| 325 | var f64_hist = std.AutoHashMap(u64, u32).init(std.testing.allocator); | ||
| 326 | defer f64_hist.deinit(); | ||
| 327 | |||
| 328 | var prng = DefaultPrng.init(0); | ||
| 329 | const random = prng.random(); | ||
| 330 | |||
| 331 | var i: usize = 0; | ||
| 332 | while (i < num_numbers) : (i += 1) { | ||
| 333 | const rand_f32 = random.float(f32); | ||
| 334 | const rand_f64 = random.float(f64); | ||
| 335 | var f32_put = try f32_hist.getOrPut(@floatToInt(u32, rand_f32 * @intToFloat(f32, num_buckets))); | ||
| 336 | if (f32_put.found_existing) { | ||
| 337 | f32_put.value_ptr.* += 1; | ||
| 338 | } else { | ||
| 339 | f32_put.value_ptr.* = 0; | ||
| 340 | } | ||
| 341 | var f64_put = try f64_hist.getOrPut(@floatToInt(u32, rand_f64 * @intToFloat(f64, num_buckets))); | ||
| 342 | if (f64_put.found_existing) { | ||
| 343 | f64_put.value_ptr.* += 1; | ||
| 344 | } else { | ||
| 345 | f64_put.value_ptr.* = 0; | ||
| 346 | } | ||
| 347 | } | ||
| 348 | |||
| 349 | var f32_total_variance: f64 = 0; | ||
| 350 | var f64_total_variance: f64 = 0; | ||
| 351 | |||
| 352 | { | ||
| 353 | var j: u32 = 0; | ||
| 354 | while (j < num_buckets) : (j += 1) { | ||
| 355 | const count = @intToFloat(f64, (if (f32_hist.get(j)) |v| v else 0)); | ||
| 356 | const expected = @intToFloat(f64, num_numbers) / @intToFloat(f64, num_buckets); | ||
| 357 | const delta = count - expected; | ||
| 358 | const variance = (delta * delta) / expected; | ||
| 359 | f32_total_variance += variance; | ||
| 360 | } | ||
| 361 | } | ||
| 362 | |||
| 363 | { | ||
| 364 | var j: u64 = 0; | ||
| 365 | while (j < num_buckets) : (j += 1) { | ||
| 366 | const count = @intToFloat(f64, (if (f64_hist.get(j)) |v| v else 0)); | ||
| 367 | const expected = @intToFloat(f64, num_numbers) / @intToFloat(f64, num_buckets); | ||
| 368 | const delta = count - expected; | ||
| 369 | const variance = (delta * delta) / expected; | ||
| 370 | f64_total_variance += variance; | ||
| 371 | } | ||
| 372 | } | ||
| 373 | |||
| 374 | // Corresponds to a p-value > 0.05. | ||
| 375 | // Critical value is calculated by opening a Python interpreter and running: | ||
| 376 | // scipy.stats.chi2.isf(0.05, num_buckets - 1) | ||
| 377 | const critical_value = 1073.6426506574246; | ||
| 378 | try expect(f32_total_variance < critical_value); | ||
| 379 | try expect(f64_total_variance < critical_value); | ||
| 380 | } | ||
| 381 | |||
| 382 | test "Random shuffle" { | ||
| 383 | var prng = DefaultPrng.init(0); | ||
| 384 | const random = prng.random(); | ||
| 385 | |||
| 386 | var seq = [_]u8{ 0, 1, 2, 3, 4 }; | ||
| 387 | var seen = [_]bool{false} ** 5; | ||
| 388 | |||
| 389 | var i: usize = 0; | ||
| 390 | while (i < 1000) : (i += 1) { | ||
| 391 | random.shuffle(u8, seq[0..]); | ||
| 392 | seen[seq[0]] = true; | ||
| 393 | try expect(sumArray(seq[0..]) == 10); | ||
| 394 | } | ||
| 395 | |||
| 396 | // we should see every entry at the head at least once | ||
| 397 | for (seen) |e| { | ||
| 398 | try expect(e == true); | ||
| 399 | } | ||
| 400 | } | ||
| 401 | |||
| 402 | fn sumArray(s: []const u8) u32 { | ||
| 403 | var r: u32 = 0; | ||
| 404 | for (s) |e| | ||
| 405 | r += e; | ||
| 406 | return r; | ||
| 407 | } | ||
| 408 | |||
| 409 | test "Random range" { | ||
| 410 | var prng = DefaultPrng.init(0); | ||
| 411 | const random = prng.random(); | ||
| 412 | |||
| 413 | try testRange(random, -4, 3); | ||
| 414 | try testRange(random, -4, -1); | ||
| 415 | try testRange(random, 10, 14); | ||
| 416 | try testRange(random, -0x80, 0x7f); | ||
| 417 | } | ||
| 418 | |||
| 419 | fn testRange(r: Random, start: i8, end: i8) !void { | ||
| 420 | try testRangeBias(r, start, end, true); | ||
| 421 | try testRangeBias(r, start, end, false); | ||
| 422 | } | ||
| 423 | fn testRangeBias(r: Random, start: i8, end: i8, biased: bool) !void { | ||
| 424 | const count = @intCast(usize, @as(i32, end) - @as(i32, start)); | ||
| 425 | var values_buffer = [_]bool{false} ** 0x100; | ||
| 426 | const values = values_buffer[0..count]; | ||
| 427 | var i: usize = 0; | ||
| 428 | while (i < count) { | ||
| 429 | const value: i32 = if (biased) r.intRangeLessThanBiased(i8, start, end) else r.intRangeLessThan(i8, start, end); | ||
| 430 | const index = @intCast(usize, value - start); | ||
| 431 | if (!values[index]) { | ||
| 432 | i += 1; | ||
| 433 | values[index] = true; | ||
| 434 | } | ||
| 435 | } | ||
| 436 | } | ||
| 437 | |||
| 438 | test "CSPRNG" { | ||
| 439 | var secret_seed: [DefaultCsprng.secret_seed_length]u8 = undefined; | ||
| 440 | std.crypto.random.bytes(&secret_seed); | ||
| 441 | var csprng = DefaultCsprng.init(secret_seed); | ||
| 442 | const random = csprng.random(); | ||
| 443 | const a = random.int(u64); | ||
| 444 | const b = random.int(u64); | ||
| 445 | const c = random.int(u64); | ||
| 446 | try expect(a ^ b ^ c != 0); | ||
| 447 | } | ||