RANDOM NUMBER GENERATOR HELP

I want to make a do while loop that has a random number generator. I also want to be able to have a keyword like 'exit' that will close the program.
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
 #include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;



int main() {
	bool game = true;

	do {

		int max, random_number;

		cout << "put big number" << endl;
		cin >> max;

		srand(time(0));
		random_number = (rand() % max) + 1;
		cout << random_number << endl;
	} while (game!=true);
		
	
	
	
	system("pause");
	return 0;
}
closed account (21vXjE8b)
First, change line 22 to game == true
Then, after line 21 add the conditional to exit, something like this:
1
2
3
4
char option;
cout << "Do you wanna exit (Y/N)? ";
cin >> option;	
if(option == 'Y') game = !game; // This will change the value of game to false and break the loop 
Last edited on
When i press 'e', it just goes crazy and repeats a bunch of times.

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
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;



int main() {
	
	bool game = true;

	do {

		int max, random_number;

		cout << "Enter a number or Enter 'e' to exit the program" << endl;
		cin >> max;

		if (max == 'e') {
			exit(0);
		}

		srand(time(0));
		random_number = (rand() % max) + 1;
		cout << random_number << endl;
		


	} while (game==true);

	
	
	
	
	system("pause");
	return 0;
}
You have declared max to be an int, if you press a letter it will error. Create another variable, something like char input. So you have one that accepts numbers, and another that accepts letters.

Also, it would be better for you to move srand (time(0)) outside of the loop, like right above or below your bool.

Hope this helps.
i'm still stuck on where to put a char variable. this loop doesnt end and i just want to be able to have an exit option. for example if they enter 'e' then the program closes.



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

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

using namespace std;



int main() {

	
	bool game = true;


	do {
		
		int random_number, max;
		cout << "Enter a number or Enter 'e' to exit the program" << endl;
		cin >> max;


			srand(time(0));
			random_number = (rand() % max) + 1;
			cout << random_number << endl;
		
	} while (game = true);







	system("pause");
	return 0;
}
closed account (E0p9LyTq)
Dump using the C library random functions, they are outdated. Even the C standard recommends not using rand() and srand().

The C++ library has much better random number generation engines and distributions:
http://www.cplusplus.com/reference/random/
(Examples galore for how to use the class templates)

With that said, try changing your current logic to look for a zero or negative number as the input to break out of your game loop.

Also move your srand() function above the start of your game loop, it should only be run once, before you start generating random numbers.

Note, line 27, you are assigning true to the game variable, which will always be evaluated as true, not checking for equality (==).

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
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
   srand(time(0));

   while (true)
   {
      cout << "Enter a number, or Enter 0 or less to exit the program: ";
      int max;
      cin >> max;
      
      if (max <= 0)
      {
         break;
      }
      
      int random_number = (rand() % max) + 1;
      cout << random_number << endl;
   }

   system("pause");
   return 0;
}


closed account (48T7M4Gy)
Typing 'e', because it is a char and not an int will cause your program to crash. You can use this to advantage as long as you use cin.clear(), cin.fail/good() and cin.ignore(0 to recover so you can continue outside the while loop.
closed account (E0p9LyTq)
Using the C++ <random> and <chrono> libraries to generate random numbers:
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
#include <iostream>
#include <random>
#include <chrono>

int main()
{
   // obtain a seed from the system clock:
   unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();

   // create a random generator, seeding it
   std::mt19937 rng(seed);  // mt19937 is a standard mersenne_twister_engine

   while (true)
   {
      std::cout << "Enter a number, or Enter 0 or less to exit the program: ";
      int max;
      std::cin >> max;

      if (max <= 0)
      {
         break;
      }

      // create a uniform int distribution, between 1 and max
      std::uniform_int_distribution<int> dist(1, max);

      // generate a random number, using the engine and distribution
      int random_number = dist(rng);
      std::cout << random_number << "\n";
   }
}


Enter a number, or Enter 0 or less to exit the program: 500
456
Enter a number, or Enter 0 or less to exit the program: 500
367
Enter a number, or Enter 0 or less to exit the program: 500
73
Enter a number, or Enter 0 or less to exit the program: 500
163
Enter a number, or Enter 0 or less to exit the program: 500
332
Enter a number, or Enter 0 or less to exit the program: 500
10
Enter a number, or Enter 0 or less to exit the program: 0
FurryGuy youre a beast. thanks.
Topic archived. No new replies allowed.