Jump to content

C++


reubenhyman

Recommended Posts

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...