4 ms·
What's the current recommendation for a better random number generator? (C please)
by plg 12y ago
What's the current recommendation for a better random number generator? (C please)
- bmm6o 12y agoIt depends in what way you want your numbers to be "better". You might mean deterministic and fast and with less predictable patterns (Mersenne Twister), deterministic and indistinguishable from true random (and slower) (CSPRNG), non-deterministic (depends on the platform).
- eveningcoffee 12y agoWhat is the usage of random numbers in your case?
- fleitz 12y agoFor most systems, open /dev/urandom and read from it. For pure C, use a secure hash function such as SHA-256. For a simpler approach, just fail to initialize the variable for which you want random data, especially if you frequently want the value to just be zero when debugging.
- mikeash 12y agoThose first two suggestions are good. The last one, not so good. First, it's not very random. As you point out, it will often just be zero. If zero is acceptable to you, then just initialize it to zero. If you need something reliably different then you can't count on uninitialized memory to deliver it. Second, it will trip diagnostics in tools like Valgrind, which you'll have to either silence or ignore. Third, and this is a real fun one, any C program which reads from uninitialized memory is non-conforming, which means that the compiler is allowed to assume that it never happens. For an example, let's say you wrote some code to flip a coin "randomly": int uninitialized; if((uninitialized % 2) == 0) printf("heads"); else printf("tails"); The compiler would be within its rights to eliminate both branches of the if statement. It would, in fact, be within its rights to start a game of nethack upon encountering such code (as early versions of gcc did when encountering a #pragma statement) or make demons fly out of your nose (a common example of what C compilers are allowed to do with such things, albeit one that's somewhat difficult to implement). Don't read uninitialized variables. It's the law!
- kbaker 12y agoMaybe arc4random(), from OpenBSD. There were some slides linked earlier about it [1], it might be worth looking into. [1] http://www.openbsd.org/papers/hackfest2014-arc4random/mgp00001.html http://www.openbsd.org/papers/hackfest2014-arc4random/mgp000...
- carey 12y agoIs it really wise to choose a random number generator based on RC4, which is known to have remarkably strong biases? Surely it would be better to use something based on ChaCha20, Keccak or Spritz if you have the choice?
- grogers 12y agoIt does use ChaCha20, and the implementation will use something else in another 20 years most likely. http://www.openbsd.org/papers/hackfest2014-arc4random/mgp00038.html http://www.openbsd.org/papers/hackfest2014-arc4random/mgp000...
- carey 12y agoServes me right for not reading through all the slides. I would argue that it's one of the more misleading function names out there, though.
- justincormack 12y agoYes, it is misleading, it used to use rc4. Posix are proposing to standardise it under a new generic name, so it is unlikely to get changed until this happens.
- emmelaich 12y agorand(3) recommends random(3) and arc4random(3) rand48(3) recommends random(3) random(3) recommends arc4random(3) So no excuses.
- nightcracker 12y agoTyche: https://eden.dei.uc.pt/~sneves/pubs/2011-snfa2.pdf https://eden.dei.uc.pt/~sneves/pubs/2011-snfa2.pdf