# always the same.....

This topic is 3652 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

ok, i am trying to do this random number game, but then i realized, the numbers it selected were always the same, how do i fix this
#include <iostream>
#include <windows.h>
using namespace std;

int main(){
int replay;
cout << "would you like to play? (click 1 to play, or 2 to stop): ";
cin >> replay;
do{
cout << "in this game, a random number will be selected, and you must guess it \n";
cout << "generating number" << "\n";

int x;
x = rand();

for(;;){
int guess;
cout << "try to guess the number: ";
cin >> guess;

if(guess == x){
MessageBox(NULL, "that was correct, good job!!!", "you win", MB_OK | MB_HELP);
break;

}
if(guess < x){
cout << "higher!" << "\n";
}
if(guess > x){
cout << "lower!" << "\n";
}
}
}
while(replay == 1);
}



##### Share on other sites
Look up the srand() function, which seeds the pseudo-random number generator for rand().

You're observing what is actually desirable behavior, that the random number generator produces the same sequence of numbers for a given seed. This is because of how the generators are implemented (they're not really random), but its a useful side-effect for things like replayability, et cetera.

Just seed the generator with srand(), using a variable value like the current tick count or time. You only need to seed the generator once.

##### Share on other sites
I suggest you read about generating random numbers in C++. You will be told about seeding the random generator with the function srand(...). If you don't seed the generator properly, then it will always come up with the same sequence of numbers.

##### Share on other sites
Try to use srand(time(NULL)) in the begin of your main function. And it would be good if you define a limit to the random number. You can do it using the % operator:

int x = rand() % MAX_RANDOM_NUMBER;

##### Share on other sites
Not entirely correct on that last post there. MAX_RANDOM_NUMBER - 1 would be more correct. Modulus is just a remainder for a division. So if you % 5 a number, it'll be between 0 and 4. Just a small technical detail.

##### Share on other sites
A better way to generate them (to offset low-order bit bias):
int x = (rand() / static_cast<float>(RAND_MAX)) * MAX_RANDOM_NUMBER;

This can be easily made into a function for arbitrary ranges.

##### Share on other sites
Honestly I can understand the original posters frustration, there are no good tutorials online (at least that I have found) that explain srand and rand. I can get them to work fine but as far as why some people set them up differently then others I have no clue on. I always did rand() % 4 + 1 (for 1 through 5). But sometimes I see things like rand() % 1 + 5 - 1 or different things like that.

##### Share on other sites
Quote:
 Original post by Chrono1081Honestly I can understand the original posters frustration, there are no good tutorials online (at least that I have found) that explain srand and rand. I can get them to work fine but as far as why some people set them up differently then others I have no clue on. I always did rand() % 4 + 1 (for 1 through 5). But sometimes I see things like rand() % 1 + 5 - 1 or different things like that.
All the manipulations of rand() are just math. Like with real math most people do things a certain way (for example, rand() % 4 + 1), but there will always be different ways of accomplishing the same thing. You'll just have to think about it; there's no magic answer here as to why people do things differently.

##### Share on other sites
The question is though whats the difference of using rand() % 5 or rand() % 4 + 1 or rand() % 1 + 5 - 1. Is there any advantage / disadvantage to doing it one way over the other? One of the previos posters talked about eliminating lower number bias, but how?

##### Share on other sites
Quote:
 Original post by Chrono1081The question is though whats the difference of using rand() % 5 or rand() % 4 + 1 or rand() % 1 + 5 - 1. Is there any advantage / disadvantage to doing it one way over the other? One of the previos posters talked about eliminating lower number bias, but how?

They are different, due to operator precedence. If, like me, you can never remember the precedence for the more obscure operators, then liberally apply brackets. I find this also helps when I read the code, as it tells me exactly what precedence I intended to use.

It all depends on what you want. Do you want random numbers from 1 to 10? 0 to 10? An arbitrary range, like 5 to 15? Floating point numbers?

rand() % 5 is 0..4
(rand() % 4) + 1 is 1..4

The last one is confusing: it varies greatly depending on how you place the brackets:

(rand() % 1) + (5 - 1) is 4..5
(rand() % (1 + 5)) - 1 is -1..4 and so on

I personally prefer to write a function that takes a range and generates a number between the range. I'll use the random calculation I used above, because it is better (works better even if RAND_MAX is small).
int random(int high, int low = 0){   int range = high - low + 1;   float f = (rand() / static_cast<float>(RAND_MAX));   return low + static_cast<int>(f * range);}

For ease of use when working with floats etc, this can be made a template:
template< class Number >Number random(Number high, Number low = 0){   Number range = high - low + 1;   float f = (rand() / static_cast<float>(RAND_MAX));   return low + static_cast<Number>(f * range);}

Again, if you are like me and you always forget which argument is high and which one is low:
template< class Number >Number random(Number high, Number low = 0){   if(high < low)   {      std::swap(high,low);   }   Number range = high - low + 1;   float f = (rand() / static_cast<float>(RAND_MAX));   return low + static_cast<Number>(f * range);}

The reason to use division is to handle small values of RAND_MAX. For example, on both gcc and VC on my machine the value of RAND_MAX is 32767. The potential values for an int are much higher. Consider trying to get values in the range 1 to RAND_MAX * 3, inclusive. The modulo method cannot generate a number above RAND_MAX + the 'low' argument. The float will. Note that it cannot generate every number between them (there aren't enough discrete values returned from rand()) so it could be argued that the distribution is no longer suitable to be called 'random'. If you just want some different numbers, then this method will suffice. Most people who need a more dependable random generator wouldn't use rand anyway.

Sometimes I get my math slightly screwed up so if someone wants to correct the above if there are errors I'd appreciate it.