logical operators and loops

im trying to write a program using the rand() function the computer has to guess the number 30 for the loop to end...can anyone help???? this is what i have so far...not much i know.
// die roller
// demonstration

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
cout << "press the enter key to exit";
cin.ignore(std::cin.rdbuf()->in_avail() + 1);

srand(time(0)); //seed random number generator based on current time

int randomNumber = rand(); //generate random number

if (randomNumber = '30')
cout << "your right\n";

return 0;
}
.not much.
If I'm reading this correctly, it looks like you want the computer to use rand() to guess the number 30. This is how you could do it:

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
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
        srand(time(0));

        int randomNumber = rand() % 100 + 1; // guess a random number 1 - 100
        int guessCount = 0; // keep track of how many guesses it takes

        while(randomNumber != 30) // loop until the computer guesses 30
        {
                randomNumber = rand() % 100 + 1; // new random number
                cout << randomNumber << endl; // print it out
                guessCount++; // increase guess count
        }

        // if it gets here, it guessed correctly
        cout << "Took " << guessCount << " guesses\n";

        return 0;
}
so your using % modulus to narrow the random number spectrum to 100? correct? is there a way i can try to get the computer to guess the number out of rand_max? or 32,767?
you can use rand()*rand() this number will have range from 0 to rand_max^2
that makes perfect sence thank you sir.
Topic archived. No new replies allowed.