Skip to content

Instantly share code, notes, and snippets.

@zmmbreeze
Last active November 21, 2018 13:28
Show Gist options
  • Save zmmbreeze/3af8142176ee4a56c1759913831fe66d to your computer and use it in GitHub Desktop.
Save zmmbreeze/3af8142176ee4a56c1759913831fe66d to your computer and use it in GitHub Desktop.
export function random(): number {
let random;
// https://zh.wikipedia.org/wiki/2147483647
// 2147483647 === 1111111111111111111111111111111
try {
const arr = new Uint32Array(1);
// https://developer.mozilla.org/en-US/docs/Web/API/RandomSource/getRandomValues
window.crypto.getRandomValues(arr);
random = arr[0] & 2147483647;
} catch (b) {
random = Math.floor(Math.random() * 2147483648);
}
return random;
}
export function uuid(join: string = '.'): string {
return random().toString(36)
+ join + random().toString(36)
+ join + random().toString(36)
+ join + (+new Date()).toString(36);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment