Can c rand give two consecutive equal numbers?

  • Thread starter Thread starter jostpuur
  • Start date Start date
  • Tags Tags
    Numbers
Click For Summary
The discussion centers on whether the C function `rand()` can return two consecutive equal numbers. It highlights that the probability of this occurring may be zero or very small, depending on the implementation of `rand()`. The Microsoft version of `rand()` is criticized for its poor performance and finite value cycling, while alternatives like the Mersenne Twister from the Boost library are recommended for better randomness. Users are advised against using `rand()` due to its limitations and potential flaws, especially in generating random numbers. Overall, the consensus is to seek more reliable random number generators for programming needs.
jostpuur
Messages
2,112
Reaction score
19
The title already contains my question.

Here's the same thing in other form, just in case the title isn't clear: Is it possible that the following function returns 1, or is it always 0?

Code:
x = rand();
y = rand();
return (x == y);

There's two possibilities. Either the probability for 1 is precisely zero, or then the probability 1 is something very small but non-zero.

I'm programming with c, and this is a rand-function that requires stdlib.h to be included.
 
Technology news on Phys.org
Without knowing how rand is implemented (and you don't), there is no way to answer this question. xkcd has a potential answer, though:

random_number.png


Don't use rand(). You can be assured that on some machines it is incredibly bad.
 
The rand function, at least the one used in Microsoft libraries, will never give the same number twice in a row, but it does cycle though a finite set of values (RAND_MAX = 0x7fff), and I'm not sure if it that set includes all numbers from 1 to 32767 (you could create a histogram array 32727 bytes long to test for missing or duplicated numbers). To create larger numbers like 32 bit numbers, I've called rand() 4 times, using bits 11->4 from the number retuned from rand() and concatenating them to create a 32 bit number.

VS has a random class that produces 32 bit numbers, but I think that only works with .NET.
 
Last edited:
That RAND_MAX=0x7fff is just one of many reasons why microsoft's rand() is bad, bad, bad. Many implementations of rand() are bad, but few are as bad as microsoft's. Look at it this way: Microsoft no longer its own implementation of rand() to generate random numbers in visual basic (and hence in Excel). They switched from rand() to a another random number generator, Wichmann-Hill, with Excel 2003. (That Wichmann-Hill is not all that good itself and that the Microsoft implementation is apparently flawed is another story.)

Jostpuur, if you have access to the boost library, they have a very good random number generator, Mersenne Twister.
 
I have Ubuntu linux. My RAND_MAX is 2^31 - 1 = 2147483647
 
jostpuur said:
I have Ubuntu linux. My RAND_MAX is 2^31 - 1 = 2147483647
Should be OK then. Most random number algorithms use the current value to produce the next value, so they shouldn't produce the same number twice in a row (if they did, they'd get stuck on that number).
 
If you don't need a huge number of random numbers, you can download one of those programs that quicky generates e or pi to millions of digits with an optional binary output, or some way to convert it to a binary file, then use that binary file as large array of random numbers. I used apfloat's (do a web search) aptest program to generate pi in hex, then converted that to binary with my own program.
 
You can also download the source code for better random number generators. The wikipedia article on the Mersenne Twister, http://en.wikipedia.org/wiki/Mersenne_twister, lists implementations in several languages. If you are using C++, the Boost libraries contain a very nice random number package; these are a part of the new (but still draft) standard. Here is the documentation on the Boost Random package: http://www.boost.org/doc/libs/1_47_0/doc/html/boost_random.html. The GNU scientific library, http://www.gnu.org/software/gsl/, also has an extensive random number package.
 
Last edited by a moderator:

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K