2 ms·
I just tested this quickly (on MacOS 10.9.2), it is not a 1,0,1,0 sequence. (It is repeatable since I'm not seeding) On Linux, same thing. It even gives the sa
by raverbashing 13y ago
I just tested this quickly (on MacOS 10.9.2), it is not a 1,0,1,0 sequence. (It is repeatable since I'm not seeding)
On Linux, same thing. It even gives the same sequence as the Mac OS version
(it's 100 numbers, "1,0,1,1...0,1," no \n at the end)
./rt | md5sum
7a5a5a0758ca83c95b21906be6052666
- mturmon 13y ago@barrkel made a small mistake, confusing rand() and random(). rand() is the earliest C random number generator. Its low-order bits (back in the day) went through a predictable sequence, so rand() & 0x1 was a bad source of random bits. I don't think that rand() was specified so fully as to make this behavior required, but typical implementations exhibited it, so you could not use rand() for any serious work. random() came after, does not use a LCG, and thus fixed this problem, so you would not see it if your code calls random(), whose man page says: The difference [between random() and rand()] is that rand() produces a much less random sequence -- in fact, the low dozen bits generated by rand go through a cyclic pattern. All of the bits generated by random() are usable. For example, `random()&01' will produce a random binary value. Typically, because of this screwup, people use a third-party generator, like the "Mersenne twister".
- barrkel 13y agoLCG can be used for serious work, depending on your definition of serious. You need to use multiplication instead of modulus.
- barrkel 13y agoMy comments were not specific to C. They were a general statement about runtime library provided random number functions across all languages - a risk to be aware of. There are other mitigations. For example, Java's RNG uses an LCG, but returns the high 32 bits and uses a 48-bit modulus to counter this weakness.