Help with assigning random integers to variables (a,b,c,etc.) using C++

This is an addition quiz that I have been working on which basically asks you questions then tells you if you were right or wrong and keeps track of your score. It asks you to continue or quit at the end of every question. My question is, in my current setup, the random numbers i get are always the same (though different each time i start the program). I want to know how i can make them different for every question but still have A+B = C so that i can determine if the answer is correct or incorrect. Any help is appreciated.

I know it looks complicated but this is probably due to my inexperience and inability to condense it.




#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{

int result;
int solution;
int score;
int play;
int question;
int end_game;
int a,b,c;
time_t seconds;
time(&seconds);
srand ((unsigned int) seconds);
a = rand() % 100 + 1;
time(&seconds);
srand ((unsigned int) seconds);
b = rand() % 100 + 1;
c = (a+b);

score = 0;
question = 1;
end_game = 0;


cout<< "This is an addition game\n";
cout<< "Type in the answer then press enter\n";
cout<< "Press 1 now to play or press 0 to quit:";


srand(time(NULL));
cin>> play;
cin.ignore();
if ( play > 0 ) { cout<< "\nGood Luck!\n" ;}

do{
do{

cout<< "\nQuestion " << question << "\n";
cout<< a << "+" << b << "=__\n";
cin>> result;
cin.ignore();
if (result == c) {cout<< "\nCorrect";
score = score + 1 ;
cout<< "\nYour current score is " << score << "\n";}

else { cout<< "\nIncorrect\n";
cout<< "The correct answer was " << c << "\n";
cout<< "Your current score is still " << score << "\n";

}

cout<< "Press 1 to continue or 0 to quit \n";
cin>> play;
cin.ignore();
if (play > 0) { question = question + 1;
cout << "\n";}



} while (play > 0);

cout<< "\n \nYour Final Score was " << score << "\n";
cout<< "Press 0 to quit or 1 to restart \n";
cin>> end_game;
cin.ignore();

} while (end_game > 0) ;}
If you seed the RNG with the same value every time, you will get the same number every time. An RNG typically should only be seeded once at program startup.


Basically...

Call srand() exactly once when the program starts. Do not call it every time you call rand().
I deleted the srand(time(Null)); and left the srand in the first chunk yet i get the same result
So you have only one srand() call and it is above all your other calls to rand()?
Yes. Here is where i stand.

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
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    int choice;
    bool gameOn = true;
    while (gameOn != false) {
    cout<< "\n  **********************\n"
    << "1-Addition Quiz Game\n"
    << "2-Subtraction Quiz Game\n"
    << "3-Multiplication Quiz Game\n"
    << "4-Division Quiz Game\n"
    << "5-Quit\n"
    << "  **********************\n"
    << "\nEnter your choise and press enter ";
    
    cin>> choice;
    
    if (choice == 1) {
    
                
           
    
    int result;
    int solution;
    int score;
    int play;
    int question;
    int end_game;
    int a,b,c;
    time_t seconds;
    time(&seconds);
    srand ((unsigned int) seconds);    
    a = rand() % 100 + 1;   
    time(&seconds);
    b = rand() % 100 + 1;
    c = (a+b);
    
    score = 0;
    question = 1;
    end_game = 0;
    
          
    cout<< "\nThis is an addition game\n";
    cout<< "Type in the answer then press enter\n";
    cout<< "Press 1 now to play or press 0 to quit:";
    
  
    
    cin>> play;
    cin.ignore();
    if ( play > 0 ) { cout<< "\nGood Luck!\n" ;}
    
  
  do{  
    do{
         
    cout<< "\nQuestion " << question << "\n";
    cout<< a << "+" << b << "=__\n";
    cin>> result;
    cin.ignore();
    if (result == c) {cout<< "\nCorrect";
    score = score + 1 ;
    cout<< "\nYour current score is " << score << "\n";}
    
    else { cout<< "\nIncorrect\n";
           cout<< "The correct answer was " << c << "\n";
           cout<< "Your current score is still " << score << "\n";
           
           } 
Your srand is in a loop, and is therefore being called multiple times.

Take it out of the loop. Very simply... do it as the very first thing in main, and then never do it again:

1
2
3
4
5
int main()
{
  srand( (unsigned)time(0) );

  // ...the rest of your program 


Also, it's possible that you are not calling rand() when you want to produce new numbers. You have a lot of loops, no comments, and inconsistent indentation, so it's difficult for me to really follow exactly what it is you're trying to do... but I suspect that you're doing something like this:

1
2
3
4
5
6
a = rand();

while( user_wants_to_keep_playing )
{
  // check 'a' here
}


The problem with this is that if the user wants to play again, 'a' will remember its old value. If you want to pick new values for 'a', you must call rand() again to select a new number.
Topic archived. No new replies allowed.