Why Does Rand() Return the Same Value Using Srand(Time(Null)) in This for Loop

Why does rand() return the same value using srand(time(null)) in this for loop?

Because you're seeding with the same value each time – time only has second-level precision, and I'm quite sure your computer can process those six loop iterations within one second. ;-]

Seed once, at the beginning of the program.

rand() generating the same number – even with srand(time(NULL)) in my main!

The issue is that the random number generator is being seeded with a values that are very close together - each run of the program only changes the return value of time() by a small amount - maybe 1 second, maybe even none! The rather poor standard random number generator then uses these similar seed values to generate apparently identical initial random numbers. Basically, you need a better initial seed generator than time() and a better random number generator than rand().

The actual looping algorithm used is I think lifted from Accelerated C++ and is intended to produce a better spread of numbers over the required range than say using the mod operator would. But it can't compensate for always being (effectively) given the same seed.

Using srand(time(NULL)) gives same output each time my program is run (not in loop, seeded with large enough values)

It's a problem of timing. The random number expression in your header is executed before the random number generator seeding code in your main function. Change your code like this

// header
int rand_number;
std::string codeword;

// source
int main() {
srand (time(NULL));
rand_number = rand() % codewords.size();
codeword = codewords[rand_number];

Now the random number is generated after the random number generator has been seeded.

PS why are the take_turn and display functions in your header file? Functions (unless they are inline) should be placed in source files.

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() not generating random numbers even after srand(time(NULL))

You must call srand() once, whereas you call it on every entry into generateWeight(). Since nowadays computers are fast and time() returns the time in seconds, this mostly restarts the random number generator from the same seed.

Why rand() returns the same values when I run my program in a script ?

The time() call has only whole-second precision. If your programs all run the same second, they will all use the same seed.

You must add more entropy. Consider using the return value of getpid() if you have it, or else investigate the platform's random sources.

Using rand() and srand(time(NULL)) right after doesn't generate a new random number

Call srand once before you start to draw random numbers

srand(time(NULL));
for (x = 1; x < 10; x++)
{

and then just make calls top rand() inside the loop:

srand(time(NULL));
for (x = 1; x < 10; x++)
{
(armoPtr + x) ->def = rand() % b + a;
if ((armoPtr + x) -> def > b)
(armoPtr + x) -> def = b;

(armoPtr + x) -> hp = rand() % 5 + 1;

(armoPtr + x) -> valOItem = rand() % (lvlpnts + 1) + max(lvlpnts);
if ((armoPtr + x) -> valOItem > (lvlpnts + 1))
(armoPtr + x) -> valOItem = (lvlpnts + 1);
(armoPtr + x) -> valOItem *= 10;
}

Feeding srand with time(NULL) more than once in the same second will make you start to draw same numbers from the beginning because time(NULL) granularity is seconds.

srand() — why call it only once?

That depends on what you are trying to achieve.

Randomization is performed as a function that has a starting value, namely the seed.

So, for the same seed, you will always get the same sequence of values.

If you try to set the seed every time you need a random value, and the seed is the same number, you will always get the same "random" value.

Seed is usually taken from the current time, which are the seconds, as in time(NULL), so if you always set the seed before taking the random number, you will get the same number as long as you call the srand/rand combo multiple times in the same second.

To avoid this problem, srand is set only once per application, because it is doubtful that two of the application instances will be initialized in the same second, so each instance will then have a different sequence of random numbers.

However, there is a slight possibility that you will run your app (especially if it's a short one, or a command line tool or something like that) many times in a second, then you will have to resort to some other way of choosing a seed (unless the same sequence in different application instances is ok by you). But like I said, that depends on your application context of usage.

Also, you may want to try to increase the precision to microseconds (minimizing the chance of the same seed), requires (sys/time.h):

struct timeval t1;
gettimeofday(&t1, NULL);
srand(t1.tv_usec * t1.tv_sec);


Related Topics



Leave a reply



Submit