scottvonduhn
02/21/2023, 5:05 PMscottvonduhn
02/21/2023, 5:19 PMbattk
02/21/2023, 5:20 PMscottvonduhn
02/21/2023, 5:22 PMbattk
02/21/2023, 5:22 PMscottvonduhn
02/21/2023, 5:25 PMAnthony OConnor
02/21/2023, 5:55 PMAnthony OConnor
02/21/2023, 6:58 PMfunction uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
Shawn Talbert
02/21/2023, 7:03 PMMath.random()
wasn't generally considered random enough for something like UUID generation?scottvonduhn
02/21/2023, 7:08 PMscottvonduhn
02/21/2023, 7:10 PMAnthony OConnor
02/21/2023, 9:06 PMMath.random()
isn't good enough for crpyto type stuff, but for collision avoidance it shouldn't be an issue... I think there's something like 4 trillion permutations, so collisions are ... unlikely... I just did what I was told by the ppl that wanted the message tagged with a UUID, they were happy with the solution.battk
02/21/2023, 11:48 PMbattk
02/21/2023, 11:50 PMAnthony OConnor
02/22/2023, 12:44 AMMath.random()
its 32 of them... for all 32 to be a match, isn't in the range of 10^4, its > 10^30Anthony OConnor
02/22/2023, 12:53 AMMath.random()
being terrible is impressive PR. I think the bigger takeaway should be don't try to make your own crypto solution, and when you need a random number Math.random()
is generally fine.
The 2 exceptions being, 1. actual crypto and 2. where you need a seed so the seed can be shared and reused and the outcome deterministic.battk
02/22/2023, 1:10 AMbattk
02/22/2023, 1:10 AMbattk
02/22/2023, 1:12 AM