Hi everyone.
I am writing a program that needs random numbers, but when I was using srand(); rand() from standard stdlib.h library I got weird results.
I've written a tiny code to find error, but still don't see any.
Using bitwise and as a faster modulo operation is a trick that only works with powers of 2. If instead of rand() you had a guaranteed power of 2 there, it would work.
The standard RNG may be not very good (I wouldn't say crap), but it's ok for small scale applications, and when uneven RN distribution isn't really a problem for you.
Using bitwise and as a faster modulo operation is a trick that only works with powers of 2. If instead of rand() you had a guaranteed power of 2 there, it would work.
Doesn't change anything if you do that.
16&4 - still 0
16&15 now 0, but that's thanks to the trivial fact that 16 & (16-(anything greater/equal 0 and lesser/equal 16)) = 0.