| ... | ... | @@ -26,16 +26,16 @@ pub const DefaultPrng = Xoroshiro128; |
| 26 | 26 | // When you need cryptographically secure random numbers |
| 27 | 27 | pub const DefaultCsprng = Isaac64; |
| 28 | 28 | |
| 29 | | pub const Rand = struct { |
| 30 | | fillFn: fn(r: &Rand, buf: []u8) void, |
| 29 | pub const Random = struct { |
| 30 | fillFn: fn(r: &Random, buf: []u8) void, |
| 31 | 31 | |
| 32 | 32 | /// Read random bytes into the specified buffer until fill. |
| 33 | | pub fn bytes(r: &Rand, buf: []u8) void { |
| 33 | pub fn bytes(r: &Random, buf: []u8) void { |
| 34 | 34 | r.fillFn(r, buf); |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | /// Return a random integer/boolean type. |
| 38 | | pub fn scalar(r: &Rand, comptime T: type) T { |
| 38 | pub fn scalar(r: &Random, comptime T: type) T { |
| 39 | 39 | var rand_bytes: [@sizeOf(T)]u8 = undefined; |
| 40 | 40 | r.bytes(rand_bytes[0..]); |
| 41 | 41 | |
| ... | ... | @@ -49,7 +49,7 @@ pub const Rand = struct { |
| 49 | 49 | |
| 50 | 50 | /// Get a random unsigned integer with even distribution between `start` |
| 51 | 51 | /// inclusive and `end` exclusive. |
| 52 | | pub fn range(r: &Rand, comptime T: type, start: T, end: T) T { |
| 52 | pub fn range(r: &Random, comptime T: type, start: T, end: T) T { |
| 53 | 53 | assert(start <= end); |
| 54 | 54 | if (T.is_signed) { |
| 55 | 55 | const uint = @IntType(false, T.bit_count); |
| ... | ... | @@ -91,7 +91,7 @@ pub const Rand = struct { |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | /// Return a floating point value evenly distributed in the range [0, 1). |
| 94 | | pub fn float(r: &Rand, comptime T: type) T { |
| 94 | pub fn float(r: &Random, comptime T: type) T { |
| 95 | 95 | // Generate a uniform value between [1, 2) and scale down to [0, 1). |
| 96 | 96 | // Note: The lowest mantissa bit is always set to 0 so we only use half the available range. |
| 97 | 97 | switch (T) { |
| ... | ... | @@ -110,18 +110,18 @@ pub const Rand = struct { |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | /// Return a floating point value normally distributed in the range [0, 1]. |
| 113 | | pub fn floatNorm(r: &Rand, comptime T: type) T { |
| 113 | pub fn floatNorm(r: &Random, comptime T: type) T { |
| 114 | 114 | // TODO(tiehuis): See https://www.doornik.com/research/ziggurat.pdf |
| 115 | 115 | @compileError("floatNorm is unimplemented"); |
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | /// Return a exponentially distributed float between (0, @maxValue(f64)) |
| 119 | | pub fn floatExp(r: &Rand, comptime T: type) T { |
| 119 | pub fn floatExp(r: &Random, comptime T: type) T { |
| 120 | 120 | @compileError("floatExp is unimplemented"); |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | /// Shuffle a slice into a random order. |
| 124 | | pub fn shuffle(r: &Rand, comptime T: type, buf: []T) void { |
| 124 | pub fn shuffle(r: &Random, comptime T: type, buf: []T) void { |
| 125 | 125 | if (buf.len < 2) { |
| 126 | 126 | return; |
| 127 | 127 | } |
| ... | ... | @@ -178,14 +178,14 @@ test "splitmix64 sequence" { |
| 178 | 178 | pub const Pcg = struct { |
| 179 | 179 | const default_multiplier = 6364136223846793005; |
| 180 | 180 | |
| 181 | | random: Rand, |
| 181 | random: Random, |
| 182 | 182 | |
| 183 | 183 | s: u64, |
| 184 | 184 | i: u64, |
| 185 | 185 | |
| 186 | 186 | pub fn init(init_s: u64) Pcg { |
| 187 | 187 | var pcg = Pcg { |
| 188 | | .random = Rand { .fillFn = fill }, |
| 188 | .random = Random { .fillFn = fill }, |
| 189 | 189 | .s = undefined, |
| 190 | 190 | .i = undefined, |
| 191 | 191 | }; |
| ... | ... | @@ -218,7 +218,7 @@ pub const Pcg = struct { |
| 218 | 218 | self.s = self.s *% default_multiplier +% self.i; |
| 219 | 219 | } |
| 220 | 220 | |
| 221 | | fn fill(r: &Rand, buf: []u8) void { |
| 221 | fn fill(r: &Random, buf: []u8) void { |
| 222 | 222 | const self = @fieldParentPtr(Pcg, "random", r); |
| 223 | 223 | |
| 224 | 224 | var i: usize = 0; |
| ... | ... | @@ -269,13 +269,13 @@ test "pcg sequence" { |
| 269 | 269 | // |
| 270 | 270 | // PRNG |
| 271 | 271 | pub const Xoroshiro128 = struct { |
| 272 | | random: Rand, |
| 272 | random: Random, |
| 273 | 273 | |
| 274 | 274 | s: [2]u64, |
| 275 | 275 | |
| 276 | 276 | pub fn init(init_s: u64) Xoroshiro128 { |
| 277 | 277 | var x = Xoroshiro128 { |
| 278 | | .random = Rand { .fillFn = fill }, |
| 278 | .random = Random { .fillFn = fill }, |
| 279 | 279 | .s = undefined, |
| 280 | 280 | }; |
| 281 | 281 | |
| ... | ... | @@ -328,7 +328,7 @@ pub const Xoroshiro128 = struct { |
| 328 | 328 | self.s[1] = gen.next(); |
| 329 | 329 | } |
| 330 | 330 | |
| 331 | | fn fill(r: &Rand, buf: []u8) void { |
| 331 | fn fill(r: &Random, buf: []u8) void { |
| 332 | 332 | const self = @fieldParentPtr(Xoroshiro128, "random", r); |
| 333 | 333 | |
| 334 | 334 | var i: usize = 0; |
| ... | ... | @@ -397,7 +397,7 @@ test "xoroshiro sequence" { |
| 397 | 397 | // Follows the general idea of the implementation from here with a few shortcuts. |
| 398 | 398 | // https://doc.rust-lang.org/rand/src/rand/prng/isaac64.rs.html |
| 399 | 399 | pub const Isaac64 = struct { |
| 400 | | random: Rand, |
| 400 | random: Random, |
| 401 | 401 | |
| 402 | 402 | r: [256]u64, |
| 403 | 403 | m: [256]u64, |
| ... | ... | @@ -408,7 +408,7 @@ pub const Isaac64 = struct { |
| 408 | 408 | |
| 409 | 409 | pub fn init(init_s: u64) Isaac64 { |
| 410 | 410 | var isaac = Isaac64 { |
| 411 | | .random = Rand { .fillFn = fill }, |
| 411 | .random = Random { .fillFn = fill }, |
| 412 | 412 | .r = undefined, |
| 413 | 413 | .m = undefined, |
| 414 | 414 | .a = undefined, |
| ... | ... | @@ -522,7 +522,7 @@ pub const Isaac64 = struct { |
| 522 | 522 | self.i = self.r.len; // trigger refill on first value |
| 523 | 523 | } |
| 524 | 524 | |
| 525 | | fn fill(r: &Rand, buf: []u8) void { |
| 525 | fn fill(r: &Random, buf: []u8) void { |
| 526 | 526 | const self = @fieldParentPtr(Isaac64, "random", r); |
| 527 | 527 | |
| 528 | 528 | var i: usize = 0; |
| ... | ... | @@ -577,8 +577,8 @@ test "isaac64 sequence" { |
| 577 | 577 | } |
| 578 | 578 | } |
| 579 | 579 | |
| 580 | | // Actual Rand helper function tests, pcg engine is assumed correct. |
| 581 | | test "Rand float" { |
| 580 | // Actual Random helper function tests, pcg engine is assumed correct. |
| 581 | test "Random float" { |
| 582 | 582 | var prng = DefaultPrng.init(0); |
| 583 | 583 | |
| 584 | 584 | var i: usize = 0; |
| ... | ... | @@ -593,18 +593,18 @@ test "Rand float" { |
| 593 | 593 | } |
| 594 | 594 | } |
| 595 | 595 | |
| 596 | | test "Rand scalar" { |
| 596 | test "Random scalar" { |
| 597 | 597 | var prng = DefaultPrng.init(0); |
| 598 | 598 | const s = prng .random.scalar(u64); |
| 599 | 599 | } |
| 600 | 600 | |
| 601 | | test "Rand bytes" { |
| 601 | test "Random bytes" { |
| 602 | 602 | var prng = DefaultPrng.init(0); |
| 603 | 603 | var buf: [2048]u8 = undefined; |
| 604 | 604 | prng.random.bytes(buf[0..]); |
| 605 | 605 | } |
| 606 | 606 | |
| 607 | | test "Rand shuffle" { |
| 607 | test "Random shuffle" { |
| 608 | 608 | var prng = DefaultPrng.init(0); |
| 609 | 609 | |
| 610 | 610 | var seq = []const u8 { 0, 1, 2, 3, 4 }; |
| ... | ... | @@ -629,14 +629,14 @@ fn sumArray(s: []const u8) u32 { |
| 629 | 629 | return r; |
| 630 | 630 | } |
| 631 | 631 | |
| 632 | | test "Rand range" { |
| 632 | test "Random range" { |
| 633 | 633 | var prng = DefaultPrng.init(0); |
| 634 | 634 | testRange(&prng.random, -4, 3); |
| 635 | 635 | testRange(&prng.random, -4, -1); |
| 636 | 636 | testRange(&prng.random, 10, 14); |
| 637 | 637 | } |
| 638 | 638 | |
| 639 | | fn testRange(r: &Rand, start: i32, end: i32) void { |
| 639 | fn testRange(r: &Random, start: i32, end: i32) void { |
| 640 | 640 | const count = usize(end - start); |
| 641 | 641 | var values_buffer = []bool{false} ** 20; |
| 642 | 642 | const values = values_buffer[0..count]; |