reubenhyman Posted February 3, 2021 Share Posted February 3, 2021 Just getting into C++ I'm experimenting with random number generator How is it that I would sometimes get the same number? srand(time(0)); I have int as int randNumb1 = 0; int randNumb2 =0; so it do this randNumb1 = (rand() % 20) +1; cout << "First Random number is " << randNumb1 << endl;; randNumb2 = (rand() % 20) +1; while (randNumb1 = randNumb2) { randNumb2 = (rand() %20) +1; cout << "Second number is " << randNumb2 << endl;; // terminate the loop break; } Link to comment Share on other sites More sharing options...
Ingolme Posted February 3, 2021 Share Posted February 3, 2021 I'm not sure if you intended to use the assignment operator on this line: while (randNumb1 = randNumb2) { You probably want the == comparison operator. You shouldn't need to break that loop, either. The cout statement should be outside the loop after the loop has finished. Link to comment Share on other sites More sharing options...
reubenhyman Posted February 3, 2021 Author Share Posted February 3, 2021 (edited) when I place the == comparison operator and comment out the break instead of = it only displays 1 number does not show second number Edited February 3, 2021 by reubenhyman Link to comment Share on other sites More sharing options...
Ingolme Posted February 3, 2021 Share Posted February 3, 2021 That is because you have the cout statement inside the while loop. Take it out of the loop and put it further down in the code. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now