Create thread-safe random
This commit is contained in:
parent
b140b20298
commit
fc704667af
1 changed files with 18 additions and 0 deletions
18
src/util.zig
18
src/util.zig
|
@ -1,6 +1,24 @@
|
|||
const std = @import("std");
|
||||
const root = @import("root");
|
||||
|
||||
threadlocal var tls_prng: ?std.rand.DefaultPrng = null;
|
||||
|
||||
fn initPrng() void {
|
||||
const higher_seed = (@bitCast(u64, std.Thread.getCurrentId()) & 0xffffffff) << 32;
|
||||
const lower_seed = @bitCast(u64, std.time.milliTimestamp()) & 0xffffffff;
|
||||
|
||||
tls_prng = std.rand.DefaultPrng.init(higher_seed | lower_seed);
|
||||
}
|
||||
|
||||
pub fn getRandom() std.rand.Random {
|
||||
if (tls_prng) |*prng| {
|
||||
return prng.random();
|
||||
}
|
||||
|
||||
initPrng();
|
||||
return tls_prng.?.random();
|
||||
}
|
||||
|
||||
pub const Uuid = struct {
|
||||
data: [16]u8,
|
||||
|
||||
|
|
Loading…
Reference in a new issue