C++ Random Numbers

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2006
Posts: 6
Reputation: Duke_0064 is an unknown quantity at this point 
Solved Threads: 0
Duke_0064 Duke_0064 is offline Offline
Newbie Poster

C++ Random Numbers

 
0
  #1
Mar 18th, 2006
Hi, I'm new to this C++ stuff. So if anyone can help that would be nice. I have this program to generate a random number and a the user inputs a number from the keyboard to correctly guess the number that the computer randomly chooses. but the numbers are always changing each time, which makes it hard to correctly guess the correct number. So the question is, is it possible to randomly choose a number without it changing each iteration?

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;

int main ( )
{
int guess;
char ans;

srand (time (0));
for (int i = 0; i < 100; i++)
{
int random = rand( );
int num = ((random % 100) + 1);



cout << "Guess a number between 1 and 100: ";
cin >> guess;

if (guess > num)
{
cout << "The number is lower. Try Again!!\n\n";
continue;

}
if (guess < num)
{
cout << "The number is higher. Try Again!!\n\n";
continue;


}
else if (guess = num)
{
cout << "You've guessed correctly!\n\n";
break;
}

}



return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 481
Reputation: Clinton Portis is on a distinguished road 
Solved Threads: 58
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Pro in Training

Re: C++ Random Numbers

 
0
  #2
Mar 18th, 2006
  1. else if (guess = num)

should be:
  1. else if (guess == num)
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 505
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 51
Bench's Avatar
Bench Bench is offline Offline
Posting Pro

Re: C++ Random Numbers

 
0
  #3
Mar 18th, 2006
Originally Posted by Duke_0064
Hi, I'm new to this C++ stuff. So if anyone can help that would be nice. I have this program to generate a random number and a the user inputs a number from the keyboard to correctly guess the number that the computer randomly chooses. but the numbers are always changing each time, which makes it hard to correctly guess the correct number. So the question is, is it possible to randomly choose a number without it changing each iteration?
Wrap your code in "Code Tags" - makes it easier for everyone to read.

Aside from Clinton's excellent spot, there is a slight flaw in your logic...

  1. //Snipped #include's
  2. int main ( )
  3. {
  4. int guess;
  5. char ans;
  6.  
  7. srand (time (0));
  8. for (int i = 0; i < 100; i++)
  9. {
So... everything after this curly brace (up til the closing brace) gets repeated 100 times.. including:
  1. int random = rand( );
  2. int num = ((random % 100) + 1);

I suggest putting these 2 lines before your loop starts, then you will only be selecting your random number once.


Also, this kind of "guessing game" would look much cleaner if you could say do....while(guess!=num) instead of for...
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 6
Reputation: Duke_0064 is an unknown quantity at this point 
Solved Threads: 0
Duke_0064 Duke_0064 is offline Offline
Newbie Poster

Re: C++ Random Numbers

 
0
  #4
Mar 18th, 2006
Thanks guys!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 3724 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC