0

Following my previous question "What algorithm is Rand() based on in C language?", the reason why I want to know the algorithm of function is I want to make a comparison of speed difference between it with RdRand.

I tried to loop it 100 million times and calculate the running time of it.

Usually, using hardware to create random numbers (RdRand) would make its speed much faster, but the result shows the otherwise (Rand() 3sec V.S. RdRand 10sec).

Could somebody help me to figure this out?

Thanks a million!

Community
  • 1
  • 1
Kudos
  • 99
  • 1
  • 3
  • 2
    Why would RdRand be faster when it is cryptographically strong and Rand is a simple LCG calculation? – Thilo May 13 '15 at 03:16
  • 1
    I'm not sure if this is a dupe or not, but it seems like it would have information of interest to you in any case: http://stackoverflow.com/questions/26771329/is-there-any-legitimate-use-for-intels-rdrand – Michael Burr May 13 '15 at 03:17
  • Here is some more discussion on RDRAND performance: http://stackoverflow.com/questions/10484164/what-is-the-latency-and-throughput-of-the-rdrand-instruction-on-ivy-bridge?rq=1 – Thilo May 13 '15 at 03:20
  • 1
    The historical version of `rand()` is a very poor linear congruential generator, where the lower bits are especially far from random. But it's very fast: just an integer multiply, add and bit masking. Usually good enough for games. Otherwise don't use it. There is some context at http://man7.org/linux/man-pages/man3/rand.3.html. Any crypto quality rng is probably quite a bit slower than rand simply because it does a lot more work scrambling bits. – Gene May 13 '15 at 03:29

1 Answers1

2

Hardware methods are not always faster than software methods.

It maybe interesting for you to look here, It's glibc's sin function versus Intel's fsin instruction which It's more accurate and faster most of the times.

Also each implementation have its own pro/cons. The RdRand main concern is:

The random number generator is compliant with security and cryptographic standards such as NIST SP 800-90A,[4] FIPS 140-2, and ANSI X9.82. --- RdRand, Wikipedia

So you can use RdRand and be sure about It's security while if you need a fast random generation and the security is not a matter. You can simply use glibc's rand().

mdh.heydari
  • 540
  • 5
  • 13
  • Security isn't the only reason you'd want a high-quality RNG. Other example uses are getting a good distribution of dice rolls in a game, or the jitter source in simulated annealing. – MooseBoys May 13 '15 at 05:26
  • There is a difference between a RNG that its behavior is hard to predict and is called secure (more formal explanation [here](http://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator)) and the distribution of a RNG. rand() probability distribution is uniform and It's simple to make other distributions from it ([Look here](http://www.emunix.emich.edu/~haynes/Papers/ProbabilityDistributions/probabilityDistributions.html)). – mdh.heydari May 13 '15 at 05:38
  • Returning the sequence `{0,1,2,3,4,...,2^32-1}` is also a uniform distribution but would be a terrible generator. `rand` isn't that bad but it's not great. glibc's is particularly bad since (according to wikipedia) it exposes the lower bits of the generated values which have particularly poor properties. – MooseBoys May 13 '15 at 06:31
  • Yeah, It's not perfect but good enough for solving many problems and It's right that It's bad for many problems like security or problems that the quality of randomness is critical. Also thanks for the discussion! :) It made me read many things that I didn't know before! – mdh.heydari May 13 '15 at 07:23