.Random() to generate GUID would be to use Date and Time with it, so it can be unique.
So, here is the Javascript code for it
function generateUUIDUsingMathRandom() { var d = new Date().getTime();//Timestamp var d2 = (performance && performance.now && (performance.now()*1000)) || 0;//Time in microseconds since page-load or 0 if unsupported return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = Math.random() * 16;//random number between 0 and 16 if(d > 0){//Use timestamp until depleted r = (d + r)%16 | 0; d = Math.floor(d/16); } else {//Use microseconds since page-load if supported r = (d2 + r)%16 | 0; d2 = Math.floor(d2/16); } return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16); }); }
56648260-754f-4f09-b256-a73124f12c21 1aa3bdae-974f-4455-97f6-316f75f20464
The Crypto
interface represents basic cryptography features available in the current context.
It allows access to a cryptographically strong random number generator and to cryptographic primitives, so here is Javascript sample code to generate GUID using it.
function uuidv4() { return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16) ); }
Here is another example
function createUUID() { var s = []; var hexDigits = "0123456789abcdef"; for (var i = 0; i < 36; i++) { s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); } s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010 s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01 s[8] = s[13] = s[18] = s[23] = "-"; var uuid = s.join(""); return uuid; }
There are many other ways, but these are best one which you can use to generate GUID using Javascript.
If you are considering any of the above solution, then I would recommend you to use, Math.Random with Datetime generator, which will make sure that no 2 GUID can be same.
ncG1vNJzZmiZnKG0rbvBmqOuqJSWwaa%2FjZympmeXqraledSuoJ1lpai2r7OMo5ivmaOYv6q802g%3D