Why Does the Rand() Return Always the Same Number

Why does rand() always return the same value?

You need to give a different seed, for example:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int a;
srand ( time(NULL) );
a = rand();
printf("%d",a);
return 0;
}

Why do I get the same result with rand() every time I compile and run?

You need to seed the rand function with a unique number before it can be used. The easiest method is to use time()

For example

srand(time(NULL));
rand();//now returns a random number

The reason is that the random numbers provided by rand() (or any other algorithm based function) aren't random. The rand function just takes its current numerical state, applies a transformation, saves the result of the transformation as the new state and returns the new state.

So to get rand to return different pseudo random numbers, you first have to set the state of rand() to something unique.

Rand() always give the same number in C

Yes, rand() by itself repeats the numbers it generated so you need to use this:

srand(time(0));  //use this line only once

//and then after that use the rand() function again

//You will also need to include time.h library above

Why does the rand() return always the same number?

Simple pseudo-random number generators actually generate a fixed sequence of numbers. The particular sequence you get is determined by the initial "seed" value. My suspicion is that you are always getting the first number in the same sequence. Therefore I suggest we try to change the sequence by calling srand every time before calling rand, thus changing the seed value every time. The docs explain that, when called without a parameter, srand generates a new seed based on current circumstances (e.g. the time on the clock). Thus you should get a difference sequence and hence a different random number:

srand
rand(200)

Now, you may ask - why are you always getting the same sequence? I have no idea! As someone else suggested in one of the comments, the behavior you are seeing is the behavior one would expect if you had other code, anywhere, that calls srand with the same, fixed value every time. So it might be good to look for that.

Why does rand() yield the same sequence of numbers on every run?

The seed for the random number generator is not set.

If you call srand((unsigned int)time(NULL)) then you will get more random results:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
srand((unsigned int)time(NULL));
cout << rand() << endl;
return 0;
}

The reason is that a random number generated from the rand() function isn't actually random. It simply is a transformation. Wikipedia gives a better explanation of the meaning of pseudorandom number generator: deterministic random bit generator. Every time you call rand() it takes the seed and/or the last random number(s) generated (the C standard doesn't specify the algorithm used, though C++11 has facilities for specifying some popular algorithms), runs a mathematical operation on those numbers, and returns the result. So if the seed state is the same each time (as it is if you don't call srand with a truly random number), then you will always get the same 'random' numbers out.

If you want to know more, you can read the following:

http://www.dreamincode.net/forums/topic/24225-random-number-generation-102/

http://www.dreamincode.net/forums/topic/29294-making-pseudo-random-number-generators-more-random/

rand() returns same values when called within a single function

You shouldn't call srand() before each call to rand(). Call it once – somewhere at the start of your program.

The problem is you restart the random generator so it starts to produce the very same pseudorandom sequence from the very same point.

rand() return the same number

It is because you are reseeding it every time, and as your program probably runs very quickly, your seed value (time()) is the same for each call as it's smallest increment is 1 second.

Try moving srand() into main() instead and call it once

Does stdlib's rand() always give the same sequence?

Yes, given the same environment for the program. From the C standard §7.20.2.2/2,

The srand function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand. If srand is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated. If rand is called before any calls to srand have been made, the same sequence shall be generated as when srand is first called with a seed value of 1.

Of course, this assumes it is using the same implementation detail (i.e. same machine, same library at the same execution period). The C standard does not mandate a standard random number generating algorithm, thus, if you run the program with a different C standard library, one may get a different random number sequence.

See the question Consistent pseudo-random numbers across platforms if you need a portable and guaranteed random number sequence with a given seed.



Related Topics



Leave a reply



Submit