Last active
March 24, 2023 23:06
-
-
Save bhb/2989e7409de9770e7be8205551eb9891 to your computer and use it in GitHub Desktop.
Revisions
-
bhb revised this gist
Mar 24, 2023 . No changes.There are no files selected for viewing
-
bhb created this gist
Mar 24, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,107 @@ const std = @import("std"); // zig version: 0.11.0-dev.2249+dcdb87836 ///////////////////////////////////////////// // zig run -O Debug struct_repro.zig // rand 1 0.47344332933425903 // rand 1 0.3766399025917053 // rand 2 0.6770572662353516 // rand 2 0.5922629833221436 // rand 3 0.8293377757072449 // rand 3 0.5863988399505615 // rand 4 0.6468727588653564 // rand 4 0.18700581789016724 // [1] 67389 abort zig run -O Debug struct_repro.zig /////////////////////////////////////////////// // zig run -O ReleaseSafe struct_repro.zig // rand 1 0.6165003180503845 // rand 1 0.09705868363380432 // rand 2 0.2942855656147003 // rand 2 0.3366544246673584 // rand 3 0.0039062497671693563 // rand 3 0.00000000118555465444814 // rand 4 0.900171160697937 // rand 4 0.3476182520389557 // rand 5 0.978439450263977 // rand 5 0.04816720262169838 /////////////////////////////////////////////// // zig run -O ReleaseFast struct_repro.zig // rand 1 0.07979004830121994 // rand 1 0.5140153765678406 // rand 2 0.5294123888015747 // rand 2 0.6598545908927917 // rand 3 0 // rand 3 0 // rand 4 0 // rand 4 0 // rand 5 0 // rand 5 0 const RNG = struct { rand: std.rand.Random, pub fn init() !RNG { 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(); return RNG{ .rand = rand }; } pub fn next(self: *RNG) f32 { return self.rand.float(f32); } }; fn rand_double1(rand: std.rand.Random) f32 { return rand.float(f32); } fn rand_double2(rng: *RNG) f32 { return rng.rand.float(f32); } fn rand_double3(rng: *RNG) f32 { return rng.next() * 2; } pub fn main() !void { 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(); // calling float directly works std.debug.print("rand 1 {d} \n", .{rand.float(f32)}); std.debug.print("rand 1 {d} \n", .{rand.float(f32)}); // passing rand to function works std.debug.print("rand 2 {d} \n", .{rand_double1(rand)}); std.debug.print("rand 2 {d} \n", .{rand_double1(rand)}); // pass the rng wrapper to a function var rng = try RNG.init(); std.debug.print("rand 3 {d} \n", .{rand_double2(&rng)}); std.debug.print("rand 3 {d} \n", .{rand_double2(&rng)}); // use next fn std.debug.print("rand 4 {d} \n", .{rng.next()}); std.debug.print("rand 4 {d} \n", .{rng.next()}); // pass to rng to function std.debug.print("rand 5 {d} \n", .{rand_double3(&rng)}); std.debug.print("rand 5 {d} \n", .{rand_double3(&rng)}); }