Create thread-safe random

This commit is contained in:
jaina heartles 2022-05-12 20:28:00 -07:00
parent b140b20298
commit fc704667af

View file

@ -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,