Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7749939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:09:42+00:00 2026-06-01T11:09:42+00:00

I am writing a game where the computer chooses a random number that a

  • 0

I am writing a game where the computer chooses a random number that a user has to guess. Then, It is the user to choose a number (he just guesses a number) that the computer has to guess. The problem is that the rand() function that i use in the second part of the game sometimes provides a number outside the new range. for instance if the new range is low=4 and high=10, rand() provides 12, which is not correct. I am trying to understand why that error, but i cannot find it. I am wrinting in Dev-C++. The code is right below followed by the output. Thanks for your help and time.

#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<time.h>




using namespace std;

int main()
{
  int computer_guess, nbr, nbrguesses, count_user_guess=0, count_computer_guess=0; 
  char user_comparison;

  int  low=1, high=99;  //initialize low to the lower boundary and high to the higher     boundary

  srand(time(0));
  nbr=rand()% 99 + 1;  //computer chooses a random number between 1 and 99...

  do
  {
    cout << "guess a number now! " << endl;
    cin >> nbrguesses;     //user enters the number he guessed...
    if ( nbrguesses>nbr)
       cout << "too big" <<endl;
    else if ( nbrguesses<nbr)
       cout << "too small" << endl;
    else if ( nbrguesses==nbr)
       cout << " you found the correct number! " <<endl;

    count_user_guess++;  //count the number of guesses from the user
  }
  while (nbrguesses!=nbr);

  cout << "you tried " << count_user_guess <<" times to find the right number " <<      endl<<endl;

  cout << "----------------------------------------------------------------------" << endl;
  cout << "Now, the computer will guess your number " << endl;

  do
  {
    srand(time(0));
    computer_guess=rand()% high + low;    //computer guesses the number
    cout << "computer guess: " << computer_guess <<endl;
    cout << "is it the correct number?" << endl;
    cin >> user_comparison;     //if user enter 
    if (user_comparison=='>')  // character '>' it means the number guessed by computer is too big
    {
       cout << "too big" <<endl;
       high= computer_guess-1;   //high is given a new boundary 
       cout << "***Current value of high = " << high << " and low = " << low << endl;    //display current boundaries

    }
    else if ( user_comparison=='<')  //the number guessed by computer is too small
    { 
      cout << "too small" << endl;
      low= computer_guess+1;  //low is given a new boundary
      cout << "***Current value of low = " << low << " and high = " << high << endl;  //display current boundaries

    }
    else if ( user_comparison=='=')
       cout << "Computer found the correct number! " <<endl;

    count_computer_guess++;  //count number of guesses from computer
  }
  while (user_comparison!='=');

  cout << "The computer tried " << count_computer_guess <<" times to find the right number " << endl;

  cout << "The Game is over now, have a good day!" << endl;
  getch();

  return 0; 

}

//**************Output******************************************

guess a number now!
50
too big
guess a number now!
25
you found the correct number!
you tried 2 times to find the right number

----------------------------------------------------------------------
Now, the computer will guess your number
computer guess: 11
is it the correct number?
>
too big
***Current value of high = 10 and low = 1
computer guess: 3
is it the correct number?
<
too small
***Current value of low = 4 and high = 10
computer guess: 12
is it the correct number?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:09:44+00:00Added an answer on June 1, 2026 at 11:09 am

    You have to replace

    computer_guess=rand()% high + low;
    

    by

    computer_guess=rand()%(high- low + 1) + low;
    

    In your example you have low = 4 and high = 10. But then you want a random number between 0 and 6 and add 4. Not one between 0 and 10, since then you could get results up to 14. If you dont want to include the high bound (Interval [low,high[ instead of [low,high] ) you have to omit the +1 in the brackets.

    Why

    rand() returns a pretty big integer number. And we want get a random number in the interval [a,b] from that.
    If we take rand()%5, we get a numer which is 0,1,2,3 or 4, so from the interval [0,4]. In general rand()%C gives a random number in the interval [0,C-1].

    If we add a constant number, e.g. Rand()%C + D, the interval is shifted: [D,C-1 +D].
    So back to the problem, we want the interval to be [a,b]. Therefor we want a=D for the lower bond and C-1+D = b for the upper. This we can transform to C = b-D+1 = b-a+1.
    So we want to use rand()%(b-a+1) + a.

    Hope this explains a bit how things work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a game server that requires connecting to a MySQL database server
A school project has me writing a Date game in C++ (example at http://www.cut-the-knot.org/Curriculum/Games/Date.shtml
I am writing a game in java and it has a grid view where
I'm making a game where the user plays against the computer. The computer opponent
I'm writing a game in Java and this is the first one that I'm
I'm writing a game and it has enemies and bullets. When a bullet hits
I am writing a game in Python (with pygame) that requires me to generate
I need implement a control solution for a computer game I am writing to
I have written a small game in 'c' that has 15 Levels. I am
I'm developing an Android game. At the moment I'm writing code that should calculate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.