Random number program please help

The program runs but I want to add a loop at the end that asks user y/n repeat program.
the program seems to generate the same random number a lot, is my number generator line correcnt? how does C++ generate the random number here?
thanks very much


Last edited on
y without quotes is a variable name
"y" in quotes is a string literal.

When you say if(quit==y) you are asking the compiler to compare two variables, one named quit and one named y. Since you don't have a variable named y is complains.

When you say if(quit=="y") you are asking the compiler to compare the variable quit with the actual string "y".


how does C++ generate the random number here?


pRNGs are basically mathematical formulas. A very simple one would look something like this:

1
2
3
4
5
6
7
int randstate;

int rand()
{
  randstate = (randstate * X) + Y;
  return randstate;
}


Where X and Y are constants chosen to produce seemingly random numbers (typically X and Y are both prime)

rand()'s internal operation may or may not be more complex than that. Other pRNGs can get incredibly complex. In fact it's an entire field of study.
thanks very much, I just need a loop at the end that asks the user to run program again y/n
Last edited on
you need the library header for the random number generator. The logic flow of the program seems to be the issue.

I think you need for the rand() function:
 
#include <cmath> 


example:
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

bool quit = false;
while (quit != true)
{
       cout << "Please Guess a number between 1 and  " << max << endl;
       cin >> guess;

       if(guess == secret)
       {
              cout << "You guessed the number!!" << endl;
              break; // this breaks out of the loop.
       }
       else
       {
             if(guess > secret)
             {
                    cout << "Your guess " << guess << " is greater than my secret number " << endl;
             }
             else
             {
                    cout << "Your guess " << guess << " is less than my secret number" << endl;
             }
       }

       cout << "Would you like to continue?"
       char response;
       cin >> response;
       if((response == 'Y') || (response == 'y') )
       {
              cout << "You a Chicken, see you later!!" << endl;
              quit = true;
       }
} // the while statemet 


this code wouldn't need the exit() function it would fall out on its own.
Last edited on
thanks so much! what a fascinating solution. what are the advantages to your solution?

I got mine to work but I kind of want it to just ask once if you want to quit. I've been trying to map out these loops with a tree diagram, but I get confused with the parantheses

with health problems I'm finding C++ challenging, can you recommend any good books or resources?
Last edited on
when I set max at 15 why does the prg print out 14 a lot?
I think rand()%MAX actually draws a value between [0,MAX[, thus not including the value MAX itself.
If instead you mean "why does 14 print more often than all other numbers?", it's because random tends to be somewhat random in its randomness.

Also, if you seed with a constant value (e.g. "srand(0)"), you'll get the same numbers on each run. If you then reseed with a constant value before every call, you'll get the same number on each call.
Topic archived. No new replies allowed.