Skip to content

Instantly share code, notes, and snippets.

@IridescentRose
Last active December 1, 2021 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save IridescentRose/b4abd421616ab579633d0e8d2ad9dba2 to your computer and use it in GitHub Desktop.
Save IridescentRose/b4abd421616ab579633d0e8d2ad9dba2 to your computer and use it in GitHub Desktop.
A simple UUID v4 Generator
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