hello i have a problem with the rand function

hello for some reason i dont really get a random resoult
i mean i run the program and i get the same result every time .
here is the code i wrote i dont get what is wrong

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main ()
{
    const int size = 4;
    int i,indx;
    char usr_ask;
    string card_type[size] = {"heart","diamond","cross","black"}; 
    vector<string> card_t(card_type,card_type+size);
    
    cout << "do you want to draw a card ? y/n";
    cin >> usr_ask;
    while (usr_ask == 'y'){
          srand(size);
          indx = rand() % size;
    
          cout << card_t[indx];
          cout << "do you want to drow another ?";
          cin >> usr_ask;
          }   
}


i have only 4 options and i onley get black which is the 3 in the array
i tried couple of ways and all of them get me the same result every time i run the program.
Last edited on
closed account (z05DSL3A)
Quick look:

srand() should only be called once in the code and use something like time(NULL) as the argument.

http://www.cplusplus.com/reference/clibrary/cstdlib/srand/
so what i shold not use seeding becouse i belive its wrong and i dont need it to reffer to time as you can see im reffering the rand to a vector which store heart, diamond, cross and black.
how can i make a random out put from this vector . im sorry but what you gave is not an answer

i called srand only once . as you can see in the code.

please someone can help me ?
you call srand() within the while loop and you're using a const value as a seed. So for each cycle the seed is the same.

Put the srand() before line 11. If you really want a different seed each time you're calling srand then do what Grey Wolf stated: srand(time(NULL));
Topic archived. No new replies allowed.