Trouble With Randomizing

I've tried to create a program that shows 25 random words and asks for you to re-type them (memory game). It works fine except the words aren't very random as every time the same order appears. I know "of" srand but don't if that will solve it or how to use it. Here is the code (There are 105 words I just deleted many of them for room):


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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <windows.h>
#include <string>

int main()
{
    using namespace std;
    
    string nouns[105] = {"advice","anger","answer","apple","arithmetic","badge", "basket","basketball","battle","beast","beetle","beggar","brain","branch",};
    
    int nounPicks, i = 0, score = 0, j;    
    int question[25];
    string answer[25];
    
    cout << "You will get 25 words to remember with two seconds betweeen each word generated.\nYour goal is to remember as many as you can\n\n";
    cout << "Press ENTER when ready\n";
    cin.get();    
    cout << "\n\n\n\n";
    
    while (i != 25)
    {
          nounPicks = (rand() % 104);
          question[i] = nounPicks;          
          cout << nouns[nounPicks] << endl; 
          Sleep(2000);                   
          i++;
    }
    
    i = 0; //so it will print out correctly
    j = 1;
    
    cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
    cout << "Please enter the words in the order you saw them.";
    while (i != 25)//to accomadate the incremented one before hand
    {       
          cout << "\n" << j << ". ";    
          cin >> answer[i];               
          i++;
          j++;
    }
        
    for (i = 0; i != 25; i++)
    {
        j = question[i];
        
        if (answer[i] == nouns[j])
        {
                      score++;
        }
    }
                      
    cout << "\nCongratualtions you are finished!";
    cout << "You scored: " << score << "/" << "25";
    
    cout << "\n\nPress ENTER to exit\n";
    cin.get();
    cin.get();
    
    return 0;      
}
          



Any help would be appreciated.
Last edited on
call srand once at the start of your program. Typically you give it the current time as the seed:

1
2
3
4
5
6
7
8
9
int main()
{
    srand( (unsigned)time(0) );

    // now rand will produce random numbers
    //  but do not call srand again -- only do it the one time

    //...
}
Thanks, it does work now but how exactly does it work?
pRNGs like rand typically work by just performing a mathematical calculation that produces seemingly random numbers.

The calculation takes an input, does some math to get the output, then uses the output as the input the next time it's called.

srand sets the initial input (ie: the "seed"). Unless you call it, the calculation is likely to start with the same initial input every time, resulting in the same calculations and the same output.

Using the time as the seed is an easy approach because the time is different every time you run the program, which means you'll get a different string of random numbers every time.

srand and rand probably look something like this (they're probably more complicated -- this is just conceptual):

1
2
3
4
5
6
7
8
9
10
11
12
unsigned rand_state = 0;

void srand(unsigned seed)
{
  rand_state = seed;
}

unsigned rand()
{
  rand_state = (rand_state * X) + Y;  // where 'X' and 'Y' are numbers which produce random-like numbers
  return rand_state;             // typically both 'X' and 'Y' are prime
}
Thanks a lot for all the help.
Topic archived. No new replies allowed.