开发者

Random number always generates 1 number

开发者 https://www.devze.com 2023-01-18 16:59 出处:网络
//Generate Food Personality for(i=0; i<food.size(); i++) { srand(time(0)); int randomFood = rand() % 6;
//Generate Food Personality
for(i=0; i<food.size(); i++)
{
    srand(time(0));     
    int randomFood = rand() % 6;

    if(randomF开发者_如何学运维ood == 1 || randomFood == 3 || randomFood == 5)
    {
        badFood.push_back(food[randomFood]); 
    }
    else if(randomFood == 0 || randomFood == 2 || randomFood == 4)
    {
              goodFood.push_back(food[randomFood]); 
    }
}
cout << "Size of Food Vector: " << food.size() << endl;
cout << "Size of Bad Food: " << badFood.size() << endl;
cout << "Size of Good Food " << goodFood.size() << endl;

randomFood is a random number through 6 and it takes the random number in food[] and adds it to a vector depending on how the random number turns out.

My problem is it seems that its always generating an odd or even number. and the bad and good.size() always prints out as 6 or 0, never anything else.


Calling srand(time(0)); at the beginning of the loop is resetting the random number generator every time through the loop. Thus, you will keep getting the same initial random number every time.

(Technically it would be possible for time(0) to return a different value between iterations of the for loop, but given the speed of today's processors it would be a very rare case when this happens for the code you have provided.)

In any case, you should call srand right before the for loop (or, better yet, just once at the beginning of your program's main routine.)


put the srand() outside of your for loop. You are always reinitializing your random seed.


The problem is that you're seeding your random number generator at the beginning of each loop. Since the loop runs so quickly, it's seeding the same time value every iteration, thus producing the same random number when you call rand.

Also, don't use use the modulo operator with random number generators; that uses the least significant bits (which are almost deterministic) rather than the leading bits (which are closer to being pseudorandom). Divide by six and truncate to an integer instead.

Finally, I'd recommend replacing the superfluous

else if(randomFood == 0 || randomFood == 2 || randomFood == 4)

with

else


Move the srand() outside of the loop.

Your loop is probably taking less than a second each time around, so time(0) is always returning the same value (its resolution is one second), so srand(time(0)) is always seeding the random number generator with the same seed, so rand() is always using the same sequence, so you get the same random number each time around the loop.


You need to move your call to srand above the for loop. This loop is executing very quickly (most likely) so time is returning the same value each time, so you are reseeding your random number generator with the same value, which will generate the same list of psudo-random numbers. You are only using the first member of the same list over and over, until the computer's clock clicks into the next second.


Call srand(time(0)) only once per thread. It shouldn't be called multiple times like in your loop.


I think it's because you are re-seeding the random number generator for each loop iteration. Move srand(time(0)); out side of the for loop

0

精彩评论

暂无评论...
验证码 换一张
取 消