Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have read so many posts on this topic:

How does rand() work? Does it have certain tendencies? Is there something better to use?

How does the random number generator work in C

and this is what I got:

1) xn+1 depends on xn i.e., previous random number that is generated.

2) It is not recommended to initialize the seed more than once in the program.

3) It is a bad practice to use rand()%2 to generate either 0 or 1 randomly.

My questions are:

1) Are there any other libraries that I missed to take a look to generate a completely random number (either 0 or 1) without depending on previous output?

2) If there is any other work around using the inbuilt rand() function to satisfy the requirement?

3) What is the side effect of initializing the seed more than once in a program?

Code snippet:

srand(time(NULL));
d1=rand()%2;
d2=rand()%2;

Here my intention is to make d1 and d2 completely independent of each other.

My initial thought is to do this:

srand(time(NULL));
d1=rand()%2;
srand(time(NULL));
d2=rand()%2;

But as I mentioned earlier which is based on other posts, this is a bad practice I suppose?

So, can anyone please answer the above questions? I apologize if I completely missed an obvious thing.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
97 views
Welcome To Ask or Share your Answers For Others

1 Answer

  1. Are there any other libraries that I missed to take a look to generate a completely random number between 0 and 1 without depending on previous output?

Not in the standard C library. There are lots of other libraries which generate "better" pseudo-random numbers.

  1. If there is any other work around using the inbuilt rand() function to satisfy the requirement?

Most standard library implementations of rand produce sequences of random numbers where the low-order bit(s) have a short sequence and/or are not as independent of each other as one would like. The high-order bits are generally better distributed. So a better way of using the standard library rand function to generate a random single bit (0 or 1) is:

(rand() > RAND_MAX / 2)

or use an interior bit:

(rand() & 0x400U != 0)

Those will produce reasonably uncorrelated sequences with most standard library rand implementations, and impose no more computational overhead than checking the low-order bit. If that's not good enough for you, you'll probably want to research other pseudo-random number generators.

All of these (including rand() % 2) assume that RAND_MAX is odd, which is almost always the case. (If RAND_MAX were even, there would be an odd number of possible values and any way of dividing an odd number of possible values into two camps must be slightly biased.)

  1. What is the side effect of initializing the seed more than once in a program?

You should think of the random number generator as producing "not very random" numbers after being seeded, with the quality improving as you successively generate new random numbers. And remember that if you seed the random number generator using some seed, you will get exactly the same sequence as you will the next time you seed the generator with the same seed. (Since time() returns a number of seconds, two successive calls in quick succession will usually produce exactly the same number, or very occasionally two consecutive numbers. But definitely not two random uncorrelated numbers.)

So the side effect of reseeding is that you get less random numbers, and possibly exactly the same ones as you got the last time you reseeded.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...