paralleldrive / cuid

Collision-resistant ids optimized for horizontal scaling and performance.
Other
3.42k stars 123 forks source link

Allow random to return full range #246

Open svanstone-resilient opened 2 years ago

svanstone-resilient commented 2 years ago

Previously only returned from 0 to 0.5. This removed 2 bits of entropy from the generated cuids. This is a replacement for Math.random() and as such it should return from 0 to 1 (but never return 1 exactly), therefore lim was also modified. The returned value now has about 30 to 32 bits of entropy. This is probably enough for this library (it gets truncated to 20 bits later), but you could consider using something like this library: https://github.com/emilbayes/secure-random-double/blob/master/index.js Which would allow you to get 51 or 52 bits of entropy into the return value.