Need help with a random number generator

Need this program to generate a random number that is then modulated. If the modulus produces a 0, then the program can continue. Otherwise it will continue to generate a random number until its modulus is 0. Then the program exits when the random number produced is 50 or less. My current code produces a runaway program that repeatedly generates the same number. What am I doing incorrect?

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 <stdlib.h>     // srand, rand
#include <time.h>       // time
#include <iostream>
using namespace std;

int main(){
	int magic_Seed_Alpha = 997;
	int randGen = 0;
        // max number that can be used for an int is 2147483647;
        // I tried using 2^21 BUT a quick cout showed the output to be
        // 23 NOT 2M
	int upper = 2000000;
	int lower = 0;

	// Intialize random seed:
	srand (time(NULL));

	// Generate random number:
	randGen = (rand() % (upper - lower+ 1)) + lower;
	cout << "Current random number: " << randGen << endl;
	
	while(randGen > 50){
		while ((randGen % magic_Seed_Alpha) != 0) {
			randGen = (rand() % (upper - lower + 1)) + lower;
		};
		cout << "Appropriate random number: " << randGen << endl;
	};
}
Last edited on
> What am I doing incorrect?
You never change randGen after the inner loop exits.
So immediately, your inner loop meets it's exit condition again and again and again....

Simple debugging by putting another cout statement inside the loop would reveal that.
Hello F95,

If you are going to use "rand" you should take time to watch this video which talks about the problems with using "rand". A better understanding of how "rand" works should help.

https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful

Andy
> I tried using 2^21 BUT a quick cout showed the output to be 23 NOT 2M
^ is the xor operator
there is no power operator, there is a `pow()' function
however you may bitshift to the left, which is equivalent to multiply by 2
so 1<< 21 === pow(2, 21)
Last edited on
Hello F95,

For the most part most of this is unchanged from what you started. I did include salem c's suggestion. Give it a couple of runs to see what it is doing. Be prepared for an endless loop.

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
#include <iostream>
#include <iomanip> // <--- setw(), fixed, showpoint and setprecision. Along with lesser known "std::quoted".
#include <stdlib.h>     // srand, rand
#include <time.h>       // time

using namespace std;

int main()
{
	constexpr int magic_Seed_Alpha = 997; // <--- As a constant the name works better as all caps.

	int randGen = 0, count{ 1 };  // <--- Added "count".

	// max number that can be used for an int is 2147483647;
	// I tried using 2^21 BUT a quick cout showed the output to be
	// 23 NOT 2M

	int upper = 2000000; // <--- If these are not meant to change they should be a constant.
	int lower = 0;
	//int upper = 50;
	//int lower = 1;

	// Intialize random seed:
	//srand(time(NULL));
	srand(static_cast<size_t>(time(nullptr)));

	// Generate random number:
	randGen = (rand() % (upper - lower + 1)) + lower;
	cout << "Current random number: " << randGen << endl;

	while (randGen > 50)
	{
		while ((randGen % magic_Seed_Alpha) != 0)
		{
			randGen = (rand() % (upper - lower)) + lower;
			//randGen = (rand() % (2000000 - 0)) + 0; // <--- So (32767 % 2000000) is 32767 + 0

			cout << std::setw(5) << count << " Appropriate random number: " << randGen << endl;

			count++;
		} // <--- Do not need the ";".

		cout << std::setw(5) << count << "    Appropriate random number: " << randGen << endl;

		count++;
	} // <--- Do not need the ";".

	return 0;  // <--- Not required, but makes a good break point.
}

If you watched the video and are using at least C++11 standards line 25 is the more appropriate way of writing "srand". If you need a better explanation let me know.

Line 36 should explain what line 35 is doing.

Line 43 is indented so that you can see the difference in the output.

When the inner while loop fails you get stuck in the outer while loop, which becomes an endless loop, because "randGen" never changes.

Reversing the comments on lines 18 - 21 will show how the program should work. Give it a test run.

Andy
Can you explain exactly what the count is used for? I ask because I ran the code with your edits and still got a runaway program. I ran it without the upper and lower constraints and the random numbers generated at line 25 were in the millions or billions (not ever coming close to 50 which is supposed to be the exit condition)
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
#include <cmath>
#include <iomanip>
#include <iostream>
#include <stdlib.h>     // srand, rand
#include <time.h>       // time
using namespace std;

int main(){
	int magic_Seed_Alpha = 997;
	int randGen = 0, count{1};
	int upper = pow(2, 21);
	int lower = 50;

	// Intialize random seed:
	//srand (time(NULL));
	srand(static_cast<size_t>(time(nullptr)));

	// Generate random number:
	randGen = rand();
	cout << "Current random number: " << randGen << endl;
	
	//while(randGen > 50){
		while ((randGen % magic_Seed_Alpha) != 0 && randGen > 50) {
			randGen = rand();
			cout << std::setw(5) << "next rand: " << randGen << endl;
			count++;
		}
		count++;
		cout << std::setw(5) << "Appropriate random number: " << randGen << endl;
	//};
	exit(0);
}
Last edited on
> Can you explain exactly what the count is used for?
It was to SHOW you that it was a runaway program by showing you how the loops worked.

It wasn't to fix anything for you, but for you to study and understand.

Which you failed to do, because you just took out the cout statements which printed it.
I figured out my problem. Thank you for the help 👌.
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 <cmath>
#include <iostream>
#include <stdlib.h>     // srand, rand
#include <time.h>       // time
using namespace std;

int main(){
	int magic_Seed_Alpha = 997;
	int randGen = 0, count = 0;
	int upper = pow(2, 11);
	int lower = pow(2, 5);

	// Intialize random seed:
	srand(static_cast<size_t>(time(nullptr)));

	// Generate random number:
	randGen = ((rand() % (upper - lower)) + lower);
	cout << "Current random number: " << randGen << endl; // testing purposes
	
	while(randGen > 50){
		while ((randGen % magic_Seed_Alpha) != 0) {
			randGen = ((rand() % (upper - lower)) + lower);
			//cout << "next rand: " << randGen << endl;
		}
		cout << "Appropriate random number: " << randGen << endl;
		randGen = (rand() % ((rand() % (upper - lower)) + lower); // Needed to generate new number, otherwise randGen never changes and while loop runs indefinitely 
	}
	exit(0);
}
Last edited on
Topic archived. No new replies allowed.