Sobeston / zig.guide

Repo for https://zig.guide content. Get up to speed with Zig quickly.
https://zig.guide
MIT License
647 stars 170 forks source link

0.13.0 Random Numbers page uses std.os.getrandom which is not yet available #260

Closed zazu7765 closed 1 month ago

zazu7765 commented 1 month ago

The first code snippet on the Random Numbers page for zig 0.13.0 uses std.os.getrandom which is not yet available... The change is to simply revert back to the posix.getrandom that we see in zig 0.12.0.

The commit change is here https://github.com/Sobeston/zig.guide/commit/14283dca1e48d247d2f91f205497d083bef5723f

test "random numbers" {
    var prng = std.rand.DefaultPrng.init(blk: {
        var seed: u64 = undefined;
        try std.os.getrandom(std.mem.asBytes(&seed));
        break :blk seed;
    });
    const rand = prng.random();

    const a = rand.float(f32);
    const b = rand.boolean();
    const c = rand.int(u8);
    const d = rand.intRangeAtMost(u8, 0, 255);

    //suppress unused constant compile error
    _ = .{ a, b, c, d };
}
image