Can c rand give two consecutive equal numbers?

  • Thread starter jostpuur
  • Start date
  • Tags
    Numbers
In summary, the conversation discusses the possibility of a function returning 1, and the use of the rand function in C to generate random numbers. The conversation concludes that the probability of the function returning 1 depends on the implementation of the rand function, and suggests alternative methods for generating random numbers such as the Mersenne Twister or using external programs. It also mentions the availability of random number packages in libraries such as Boost and GNU scientific library.
  • #1
jostpuur
2,116
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
  • #2
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.
 
  • #3
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:
  • #4
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.
 
  • #5
I have Ubuntu linux. My RAND_MAX is 2^31 - 1 = 2147483647
 
  • #6
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).
 
  • #8
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.
 
  • #9
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:

Related to Can c rand give two consecutive equal numbers?

1. What is c rand?

C rand is a function in the C programming language that generates pseudo-random numbers. It is commonly used in scientific and mathematical applications to simulate randomness.

2. How does c rand work?

C rand uses a linear congruential algorithm to generate random numbers. This algorithm uses a seed value, which is an initial value that determines the sequence of generated numbers. Each time the function is called, the seed value is updated to ensure a different sequence of numbers is generated.

3. Can c rand give two consecutive equal numbers?

Yes, it is possible for c rand to generate two consecutive equal numbers. However, the probability of this happening is very low and depends on the seed value and the range of numbers being generated.

4. How can I prevent c rand from generating two consecutive equal numbers?

To prevent c rand from generating two consecutive equal numbers, you can use a different seed value each time the function is called. This can be achieved by using a time-based seed or by using a counter that increments each time the function is called.

5. Are there alternative functions to c rand that do not generate consecutive equal numbers?

Yes, there are other functions that can be used to generate random numbers in C, such as the random and srand functions. These functions use more complex algorithms and are less likely to generate consecutive equal numbers. However, they may have other limitations or trade-offs compared to c rand.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top