views:

202

answers:

7
//Generate Food Personality
for(i=0; i<food.size(); i++)
{
    srand(time(0));     
    int randomFood = rand() % 6;

    if(randomFood == 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.

+10  A: 

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.)

fbrereto
+4  A: 

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

Benoit
+1  A: 

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

Donotalo
+2  A: 

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.

CanSpice
+1  A: 

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

GWW
+3  A: 

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
Seth Johnson
Comment that got all three things I noticed. I'd personally do a case 0;2;4...break; default;, but anything to remove unneeded fluff.
WernerCD
+2  A: 

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.

nategoose