Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generate a random 32 bit unsigned integer
function randomUint32() {
if (window && window.crypto && window.crypto.getRandomValues && Uint32Array) {
var o = new Uint32Array(1);
window.crypto.getRandomValues(o);
return o[0];
} else {
console.warn('Falling back to pseudo-random client seed');
return Math.floor(Math.random() * Math.pow(2, 32));
}
}
@bryc

This comment has been minimized.

Copy link

@bryc bryc commented Jan 15, 2019

Some one-liners:

// crypto
window.crypto.getRandomValues(new Uint32Array(1))[0];
// Math.random
(Math.random()*4294967296)>>>0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment