### While Loop, and generating random numbers

so i have a while loop, each time it runs i want it to take in account each previous number that was randomly selected. When i run the loop i want it to create a new number every time the user says so, and at the same time taking in account the new numbers generated in the loop already, This code doesnt work, it wont take into account the new numbers generated. So i have this:

int num1, num2, num3;
num1 = 10;
num2 = 4;
num3 = 66;

int num4;
char hit;
cout <<"Would you like another number? enter 'y' or 'n': " << endl;
cin >> hit;
cout << endl;
while (hit =='Y' || hit == 'y')
{
num4 = rand() % 51 + 1;
while (num4 == num1 || num4 == num2 || num4 == num1){
num4 = rand() % 52 + 1;
} //end of inner loop

cout << num4;
cout <<"Would you like another number? enter 'y' or 'n': " << endl;
cin >> hit;
} //end of loop

so the trouble lies in generating a fifth number that will not possibly be the 4th number
do you have your full code? and could you please use the <> code thing in the format making it easier to read

Also the reason you cannot get a different 5th number is becuase there are no such as a real random number. There are only psuedo random numbers.

if you want to get "good" psuedo random numbers
use

`#include <ctime> `

`srand(time(NULL)); //this will give you a new random number in your program every time you run it. `

then to generate new random number within the program create a variable to hold the "random" number and multiply by an interger then multiply it by % " what ever you want here"

this what it might look like

 ``12`` ``````ran = (rand() * 33); //storing rand() * 33 into the variable ran (this will give you a high integer value that is seemingly random ran = (rand() % 100 ) + 1; //this will take the integer value you just generated mod 100 + 1 to give you a "random" number between 1-100 ``````
Last edited on
if this was somewhat conusing here is a program i made that will generate a random number 10,000 times that is bewteen 1-100 and will store any number less than 75 into counter A and any number greater than 75 into counter B

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include #include #include using namespace std; int main() { srand(time(NULL)); int ran, count, countA, countB; countA = 0; countB = 0; count = 0; do { count = count +1; ran = (rand() * 33); ran = (rand() % 100) + 1; if (ran < 75) { countA = countA + 1; } else { countB = countB + 1; } } while (count < 10000); cout << "countA: " << countA << endl; cout << "countB: " << countB << endl; getch(); return 0; } ``````

The results should be roughly

countA: ~7500
countB: ~2500
Last edited on
Here is my attempt at generating only unique numbers. It doesn't ever repeat the same number twice in a row, it also allows the user to contiguously generate new unique numbers until they type anything other than 'y'.

You can change the number after the modulo symbol to set the range to any amount of numbers you wish ( i.e... randNum.push_back(1 + rand() % 5000); ) , I just used 2 as a test.

 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include #include #include #include using namespace std; int main() { vector randNum; char hit = 'y'; int n = 0; srand((unsigned)time(0)); while (hit =='Y' || hit == 'y') { randNum.push_back(1 + rand() % 2); if (randNum.size() > 1){ for (int i=0; i < randNum.size(); i++){ while (randNum[n] == randNum[n-1]) { randNum.pop_back(); randNum.push_back(1 + rand() % 2); cout << "New number generated because original matched previous number" << endl; } } } cout << n << ".) Generated number is: " << randNum[n] << endl; cout << "Would you like to generate another number? (y or n): "; cin >> hit; n++; } //end of loop cout << endl << endl << "Numbers" << endl << "----------------" << endl; for (int j=0; j
Last edited on
Topic archived. No new replies allowed.