Skip to content

Instantly share code, notes, and snippets.

View Ensamisten's full-sized avatar

Ensamisten Ensamisten

  • 01:39 (UTC +01:00)
View GitHub Profile
@Ensamisten
Ensamisten / randomUUID.js
Created October 5, 2023 10:39 — forked from unascribed/randomUUID.js
A simple and readable way to generate valid v4 UUIDs in JavaScript. CC0 https://creativecommons.org/publicdomain/zero/1.0/
function randomUUID() {
return randhex(8) + "-" + randhex(4) + "-4" + randhex(3) + "-" + choice(["8", "9", "a", "b"]) + randhex(3) + "-" + randhex(12);
}
let scratchTarr = new Uint32Array(1);
function choice(arr) {
crypto.getRandomValues(scratchTarr);
return arr[Math.floor(scratchTarr[0]%arr.length)]
}
function randhex(count) {
crypto.getRandomValues(scratchTarr);