Skip to content

Instantly share code, notes, and snippets.

@RHavar
Created July 31, 2015 14:48
Show Gist options
  • Save RHavar/a6511dea4d4c41aeb1eb to your computer and use it in GitHub Desktop.
Save RHavar/a6511dea4d4c41aeb1eb to your computer and use it in GitHub Desktop.
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
Copy link

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