I am making a Flash Card program and i am includeing a shuffling feature. When the user presses Space, the program will create a vector with a list of random numbers. There should be no repeats in the vector so that you get to see every card. The problem is, i can't seem to find the problem. Here is the portion of the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
if(GetAsyncKeyState(VK_SPACE))
{
for(int sdck = 1; sdck <= deck.words; sdck++)
{
x = ((rand()%deck.words) + 1);
if(x == 0)
{
x++;
}
if(sdck != 1)
{
for(int dk = 1; dk <= sdck; dk++)
{
while(x == shuffle[dk])
{
x = ((rand()%deck.words) + 1);
if(x == 0)
{
x++;
}
}
}
}
shuffle.push_back(x);
}
|
what it should do is make a random number, check the vector (assuming we already have a value in the vector, hence while( != 1)) to see if we already have the same number entered, and if so create a new random number.
shuffle[0] is already initialized (though not shown in the post), so we start at 1, and work our way to deck.words (deck.words = the number of cards in our deck).
When the code has found a number not already in the vector, it should put it in. For some reason, though, I keep getting duplicate numbers. I can't figure this out, since the while loop should theoretically eliminate the possability of that... but i may be wrong. This is the first program i have written that incorporates a "rand()" function, so I'm not entirely educated on the behavior of the function or how it chooses it's numbers. I will say this though, i have seen some discrepenc ies in the randomizations. One such example is that whenever i exit the program and restart it, i will make it create a random number and the first number is always the same: 10.
So, long story short, I need help. lol