Faster than rand ()?
I am working on an algorithm that should generate millions of numbers as quickly as possible. In fact, I found out that the rand () function of my algorithm takes 75% of the process time.
So, I'm looking for something faster. And I don't need a large range. (I only want integers below 1000)
Do you know something that I could use?
Thank!
Edit:
I am using these numbers to shuffle groups of less than 1000 objects.
I learned more about fast rand. And there is a version of SSE version that is even faster and generates 4 numbers at a time.
source to share
The Mersen Twister algorithm is a fairly fast but balanced pseudo-random number generator.
Here's an example implementation: http://fmg-www.cs.ucla.edu/geoff/mtwist.html
source to share
On most systems, rand () is a pseudo-random number generator. So the code needs to be just a few shift + bit-OR operations and is capable of producing millions of numbers per second on a regular PC. You don't say what you get, what your hardware is, or what C library you are using, so itβs hard to see why your implementation is "slow".
Perhaps you can try to reuse the bits: take the least significant ten bits (= 1024 values), modulo 1000, to get the number range you want. Then shift and when you're done with a bit, call rand()
again for more bits.
source to share
If you are using an Intel Ivy Bridge processor, you can offload the random number generation to the hardware very well using RDRAND .
This Stack Overflow article talks about RDRAND throughput.
You can also determine if the processor supports RDRAND and uses hardware offloading and also opt out of software implementation.
source to share