I am writing some embedded code in C and need to use the rand() function. Unfortunately, rand() is not supported in the library for the controller. I need a simple implementation that is fast, but more importantly has little space overhead, that produces relatively high-quality random numbers. Does anyone know which algorithm to use or sample code?
EDIT: It's for image processing, so "relatively high quality" means decent cycle length and good uniform properties.
Use the C code for LFSR113 from L'écuyer:
Very high quality and fast. Do NOT use rand() for anything. It is worse than useless.
There is one simple RNG named KISS, it is one random number generator according to three numbers.
Also there is one web site to test RNG http://www.phy.duke.edu/~rgb/General/dieharder.php
I recommend the academic paper Two Fast Implementations of the Minimal Standard Random Number Generator by David Carta. You can find free PDF through Google. The original paper on the Minimal Standard Random Number Generator is also worth reading.
Carta's code gives fast, high-quality random numbers on 32-bit machines. For a more thorough evaluation, see the paper.
Better yet, use multiple linear feedback shift registers combine them together.
Assuming that
sizeof(unsigned) == 4
:The standard solution is to use a linear feedback shift register.
Mersenne twister
A bit from Wikipedia:
It passes numerous tests for statistical randomness, including the Diehard tests. It passes most, but not all, of the even more stringent TestU01 Crush randomness tests.
source code for many languages available on the link.