Looking at the code in avr-libc for generating random numbers, I notice a few things:https://github.com/vancegroup-mirrors/avr-libc/blob/master/avr-libc/libc/stdlib/rand.c
It's an LCG...
It requires 3 longs of stack space
There's 3 multiplies, a division, 3 modulos, and 4 multiplies, along with 2 pointer dereferences, which I hope are optimized out
For better quality randomness, they are using an LCG modulo 2^31, making the period 2^32-1, half what you should be able to get in 32 bits.
Just out of curiosity, what is going on here? I know 32-bit shifts are expensive without a barrel shifter, but it still seems like a marsaglia xorshift generator, which looks like y^=y<<2;y^=y>>7;y^=y<<7; should be faster.
I'll do some tests tomorrow, but I'm kinda doubtful of the quality of any LCG. I'd imagine there was some good reason for using that particular RNG, but I don't have a clue what it was, since imho 12 bytes of stack is unacceptable to generate one random number.
The xor shift could use as little as 1 byte of stack depending on how gcc handles it.
What do you guys think?