2

Would like to generate some random strings to be used as database keys.

Essentially these will be UUIDs, however they do not need to conform to any particular spec aside from the necessary 122-128 bits of randomness.

Most of the code will run in a browser. As I understand it, Javascript running with a browser cannot be trusted to properly seed its random number generator. Instead I was hoping to seed the random number generator with some random data from the server, then generate multiple "UUIDs" starting from that seed.

Is there a good random number generator that can be used for this purpose?

Chris Dutrow
  • 48,402
  • 65
  • 188
  • 258
  • Try [crypto.getRandomValues](https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues) and falling back on Math.random – megawac Apr 21 '14 at 02:01
  • I would recommend using a server side component for reliable random things in JavaScript. – Ja͢ck Apr 21 '14 at 02:07
  • Random and Unique are hard to get together (short of shuffling, but it gives you limited number of items). Consider if simple auto-increment (with possible server-side range allocation) is enough. – Alexei Levenkov Apr 21 '14 at 02:07
  • For true random number generation you should use the WebAPI provided by http://random.org/ – Aadit M Shah Apr 21 '14 at 02:15
  • (Math.random() * performance.now()).toString(36) – dandavis Apr 21 '14 at 03:36

0 Answers0