| ... | @@ -3,21 +3,22 @@ | ... | @@ -3,21 +3,22 @@ |
| 3 | // This file is part of [zig](https://ziglang.org/), which is MIT licensed. | 3 | // This file is part of [zig](https://ziglang.org/), which is MIT licensed. |
| 4 | // The MIT license requires this copyright notice to be included in all copies | 4 | // The MIT license requires this copyright notice to be included in all copies |
| 5 | // and substantial portions of the software. | 5 | // and substantial portions of the software. |
| 6 | // The engines provided here should be initialized from an external source. For now, randomBytes | 6 | |
| 7 | // from the crypto package is the most suitable. Be sure to use a CSPRNG when required, otherwise using | 7 | //! The engines provided here should be initialized from an external source. For now, randomBytes |
| 8 | // a normal PRNG will be faster and use substantially less stack space. | 8 | //! from the crypto package is the most suitable. Be sure to use a CSPRNG when required, otherwise using |
| 9 | // | 9 | //! a normal PRNG will be faster and use substantially less stack space. |
| 10 | // ``` | 10 | //! |
| 11 | // var buf: [8]u8 = undefined; | 11 | //! ``` |
| 12 | // try std.crypto.randomBytes(buf[0..]); | 12 | //! var buf: [8]u8 = undefined; |
| 13 | // const seed = mem.readIntLittle(u64, buf[0..8]); | 13 | //! try std.crypto.randomBytes(buf[0..]); |
| 14 | // | 14 | //! const seed = mem.readIntLittle(u64, buf[0..8]); |
| 15 | // var r = DefaultPrng.init(seed); | 15 | //! |
| 16 | // | 16 | //! var r = DefaultPrng.init(seed); |
| 17 | // const s = r.random.int(u64); | 17 | //! |
| 18 | // ``` | 18 | //! const s = r.random.int(u64); |
| 19 | // | 19 | //! ``` |
| 20 | // TODO(tiehuis): Benchmark these against other reference implementations. | 20 | //! |
| | 21 | //! TODO(tiehuis): Benchmark these against other reference implementations. |
| 21 | | 22 | |
| 22 | const std = @import("std.zig"); | 23 | const std = @import("std.zig"); |
| 23 | const builtin = @import("builtin"); | 24 | const builtin = @import("builtin"); |
| ... | @@ -29,10 +30,10 @@ const math = std.math; | ... | @@ -29,10 +30,10 @@ const math = std.math; |
| 29 | const ziggurat = @import("rand/ziggurat.zig"); | 30 | const ziggurat = @import("rand/ziggurat.zig"); |
| 30 | const maxInt = std.math.maxInt; | 31 | const maxInt = std.math.maxInt; |
| 31 | | 32 | |
| 32 | // When you need fast unbiased random numbers | 33 | /// Fast unbiased random numbers. |
| 33 | pub const DefaultPrng = Xoroshiro128; | 34 | pub const DefaultPrng = Xoroshiro128; |
| 34 | | 35 | |
| 35 | // When you need cryptographically secure random numbers | 36 | /// Cryptographically secure random numbers. |
| 36 | pub const DefaultCsprng = Isaac64; | 37 | pub const DefaultCsprng = Isaac64; |
| 37 | | 38 | |
| 38 | pub const Random = struct { | 39 | pub const Random = struct { |