Random Number Generator Interval
Nov 13, 2009 at 9:00pm UTC
I have this code , so the point is when the pc choose a random number and it's too small/big , it sets to be the edge of the interval of generating a random number.How do i 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand(time(0));
int number = rand() ;
int theNumber = 0 ;
int guess = 0 ;
int counter = 0 ;
char choise;
do
{
cout << " Write your number : " ;
cin >> guess ;
cout << endl;
do
{
theNumber = (rand() % guess ) + 1 ;
counter++;
cout << "Pc guess is: " << theNumber << endl;
if ( guess > theNumber)
cout<< "It's too big " << endl;
if (guess < theNumber)
{
cout << " It's too small ! " << endl;
}
cout << "=======================" <<endl;
}
while ( guess != theNumber);
cout << "That's the number ! " << endl;
cout << counter << " tries used" << endl ;
cout <<"repeat the program ?(y/n) : " ;
cin >> choise ;
}
while (choise == 'y' );
system("pause" );
return 0 ;
}
Nov 13, 2009 at 11:58pm UTC
You need to generate a random number within a certain range. That range will have a lower and upper boundary.
If the computer guesses too low, you adjust the low boundary.
If it guesses too high, you adjust the high boundary.
To get a number within that range:
int computerguess = rand() % (high - low) + low;
That gets you a number in range [low-high).
Nov 14, 2009 at 7:39pm UTC
thanks for help.
Topic archived. No new replies allowed.