Last active
December 1, 2021 13:52
-
-
Save IridescentRose/b4abd421616ab579633d0e8d2ad9dba2 to your computer and use it in GitHub Desktop.
A simple UUID v4 Generator
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 characters
const std = @import("std"); | |
const chars : []const u8 = "0123456789ABCDEF"; | |
pub const UUID = struct{ | |
const Self = @This(); | |
id: [36]u8, | |
pub fn new(seed: u64) !*Self{ | |
var r = std.rand.DefaultPrng.init(seed); | |
var uu = try std.heap.page_allocator.create(Self); | |
var i : usize = 0; | |
while(i < 36) : (i += 1){ | |
var res : u8 = r.random.uintLessThanBiased(u8, 16); | |
uu.id[i] = chars[res]; | |
} | |
uu.id[8] = '-'; | |
uu.id[13] = '-'; | |
uu.id[14] = '4'; | |
uu.id[18] = '-'; | |
uu.id[23] = '-'; | |
return uu; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment