The easiest thing you can do, short of writing another PRNG or using a library, would be to just use all bits that a single call to rand()
gives you. Most random number generators can be broken down to a stream of bits which has certain randomness and statistical properties. Individual bits, spaced evenly on that stream, need not have the same properties. Essentially you're throwing away between 14 and 31 bits of pseudo-randomness here.
You can just cache the number generated by a call to rand()
and use each bit of it (depending on the number of bits rand()
gives you, of course, which will depend on RAND_MAX
). So if your RAND_MAX
is 32768 you can use the lowest-order 15 bits of that number in sequence. Especially if RAND_MAX
is that small you are not dealing with the low-order bits of the generator, so taking bits from the high end doesn't gain you much. For example the Microsoft CRT generates random numbers with the equation
xn + 1 = xn · 214013 + 2531011
and then shifts away the lowest-order 16 bits of that result and restricts it to 15 bits. So no low-order bits from the generator there. This largely holds true for generators where RAND_MAX
is as high as 231 but you can't count on that sometimes (so maybe restrict yourself to 16 or 24 bits there, taken from the high-order end).
So, generally, just cache the result of a call to rand()
and use the bits of that number in sequence for your application, instead of rand() % 2
.