Skip to content

Commit

Permalink
fix: replace DefaultPrng with std.crypto.random
Browse files Browse the repository at this point in the history
  • Loading branch information
jiacai2050 committed Jan 2, 2024
1 parent e4aa67a commit 0094991
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 6 deletions.
6 changes: 2 additions & 4 deletions book-src/06-01-rand.md
@@ -1,11 +1,9 @@
## Generate random numbers

Generates random numbers with help of random-number generator [`std.rand.Random`].

Currently the default pseudorandom number generator(PRNG) used in Zig is [Xoshiro256++](https://prng.di.unimi.it/).
Generates random numbers with help of a thread-local, cryptographically secure pseudo random number generator [`std.crypto.random`].

```zig
{{#include ../src/06-01.zig }}
```

[`std.rand.random`]: https://ziglang.org/documentation/0.11.0/std/#A;std:rand.Random
[`std.crypto.random`]: https://ziglang.org/documentation/0.11.0/std/#A;std:crypto.random
3 changes: 1 addition & 2 deletions src/06-01.zig
Expand Up @@ -2,8 +2,7 @@ const std = @import("std");
const print = std.debug.print;

pub fn main() !void {
var prng = std.rand.DefaultPrng.init(@intCast(std.time.timestamp()));
const rand = prng.random();
const rand = std.crypto.random;

print("Random u8: {}\n", .{rand.int(u8)});
print("Random u8 less than 10: {}\n", .{rand.uintLessThan(u8, 10)});
Expand Down

0 comments on commit 0094991

Please sign in to comment.