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