// Do not use. Use Symbol() instead. |
Thanks :)
Gracias
thanks! works great
Another way which uses a cryptographically secure random number generator, in UUID format.
var ID = () => { let array = new Uint32Array(8) window.crypto.getRandomValues(array) let str = '' for (let i = 0; i < array.length; i++) { str += (i < 2 || i > 5 ? '' : '-') + array[i].toString(16).slice(-4) } return str }
This comment is the answer. It's from Mozilla Developer MDN Web Docs and supported by all kinds of browsers out there on both computer and mobile including Node.js server.
Thanks.
I would go for
return ('000000000' + Math.random().toString(36).substr(2, 9)).slice(-9);
This would always return 9 digits. If the random number happens to be 0.0
, the original code would return '_'
.
My version would return 000000000
....
I would go for
return ('000000000' + Math.random().toString(36).substr(2, 9)).slice(-9);This would always return 9 digits. If the random number happens to be
0.0
, the original code would return'_'
.My version would return
000000000
....
Does it pass this duplicate key test?
The following code block can also be the answer for generating a unique id. It's in the same comment towards the end after checking the duplicates
function uniqueID(){
function chr4(){
return Math.random().toString(16).slice(-4);
}
return chr4() + chr4() +
'-' + chr4() +
'-' + chr4() +
'-' + chr4() +
'-' + chr4() + chr4() + chr4();
}
uniqueID() // "e27881c4-f924-b8f7-59d9-525878c7a812"
// NOTE: This format of 8 chars, followed by 3 groups of 4 chars, followed by 12 chars
// is known as a UUID and is defined in RFC4122 and is a standard for generating unique IDs.
// This function DOES NOT implement this standard. It simply outputs a string
// that looks similar. The standard is found here: https://www.ietf.org/rfc/rfc4122.txt
thank you, I had been looking for ways to generate unique ids. finally found the better solution!
boi o boi thats way to great for noob like me...
Thanks...
what about this
return new Date().getTime().toString(36).concat(performance.now().toString(), Math.random().toString()).replace(/\./g,"");
Thanks
Can anyone reduce the length of the string by 5-6 Characters still maintaining the collision factor ?
Tha
what about this
return new Date().getTime().toString(36).concat(performance.now().toString(), Math.random().toString()).replace(/\./g,"");
Thanks this was helpful
Amazing! So useful. THX!