Random Number problems

Started by
14 comments, last by Ezbez 18 years, 6 months ago
Hello, I'd like to be able to do a random interger from, say, minNum to maxNum in c++. Those would both be varaibles. I tried this code: int number = rand()%(maxNum-minNum)+minNum; But it didn't work. Its a little hard to tell what range of number that it did get it for since I emidiatly put it through a sin() function. It does compile without an error and the program doesn't crash or anything. Hopefully that is enough information. Thanks in advance!
Advertisement
There was a thread on this exact subject a few weeks ago. Try and do a forum search on "random" or "random numbers" and you should be able to find it (and probably a great deal more).
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
To create generate a random number between 1 and 10 you would use rand()%10+1. To generate a random number between 600 and 750 you would use rand()%750+1. Using something like rand()%100 would give you a random number between 0 and 99, because the null "\0" takes up one slot.

rand() will only give you a random number at comiletime though, so if it generates 15 the first time, everytime you run the program it will still generate 15. Using the ctime header and the srand() function you can achieve run-time random numbers. Here's an example of how to:
#include <iostream> // Text I/O#include <cstdlib> // Rand and srand#include <ctime> // Time func in srandusing namespace std;int main(){    srand((unsigned)time(NULL)); // Set srand using system time    cout<<rand%10+1; // Print random number between 0 and 10. We could have also used rand%11 here.}
Thanks for the replies.

Stylin: I found that post, but when I used the code, it did not work. I used:
rand () * (max-min) / RAND_MAX) + min;

But that generates random numbers at intervals for some reason. It seemed that it was only giving me multiples of about 5 when I used between 10 and 45.

UndeadInsanity: I'm trying to use variables to determine the range, not preset values.

Okay, heres the whole story:

I'm trying to make random number in a range of angles. This is for some explosions in my game. So sometimes I may want a full complete explosion that goes all the way from 0 degrees to 360 degreees. Other times I may want to just use from 10 degrees to 45 degrees. I would like to use the same function for both of those purposes, and not have to have many seperate ones.

I just made it so that I output(using printf(); ) the randomly generated number, but somethings messed up. Every single output is 2292944. Obviously, this isn't inside of my range. Anyone know whats happening?

What makes this even more mysterious is that while in the game, it appears to be generating random numbers.
The above has some errors. Your equation is correct, to genearate number between 600 and 750 you would have

int number = rand()%(750-600)+600;

which would include 600, but not 750, so you'd have 600-749. rand() returns numbers from 0-RAND_MAX, where RAND_MAX is usually equal to the largest number you can put in a signed int (0x7FFFFFFF). Also 'null "\0"' is nonsense, but I assume he's just talking about 0 possibly being returned from rand(). Finally, rand() does not generate numbers at compile time. Every call to rand() will return a different number, but if you don't use srand(), you'll get the same sequence of numbers everytime you run your program. As for you error, my only guess is that sin takes values in radians and you're passing it values in degrees.

tj963

EDIT:
rand () * (max-min) / RAND_MAX) + min;

doesn't really work because of precision issues, and I would use the one above. The distribution is not quite uniform, but unless your creating random numbers from on a range of of millions, or are writing some sort of statistics package I wouldn't worry. Also, my comment about degrees/radians is your problem. As for printf, you might have incorrect parameters.
tj963
I did something similar, maybe this is what you're looking for?

srand((unsigned int)time(NULL)); //seed rand() with timevoid RandomRange (int& min, int& max){    int a = 0;    int b = 0;        while (a==b) //So that there won't be a range between the same numbers.    {	a = rand() % (360 - 1);        b = rand() % (360 - 1);	        if (a > b)        {            max = a;            min = b;        }        else        {            max = b;            min = a;        }    }}


Nice small tutorial
Or you can use boost::random and not worry about implementation details.
Also beware of the quality of random numbers that come from rand(). In some cases RAND_MAX is defined as 0x7fff and the period/distribution of the numbers can be poor depending on the implementation. Quite a lot of the time this doesn't matter but it can cause problems when you start using the modulo ('%') operator which just amplifies the poor quality of the numbers.

arm.
Quote:Original post by arm
Also beware of the quality of random numbers that come from rand(). In some cases RAND_MAX is defined as 0x7fff and the period/distribution of the numbers can be poor depending on the implementation. Quite a lot of the time this doesn't matter but it can cause problems when you start using the modulo ('%') operator which just amplifies the poor quality of the numbers.

arm.


I didn't quite get that. Would you or anybody else be so kind to explain how that works and / or affects 'poor quality'? What does 'poor quality' mean in this context?
Calm yourself,

By 'poor quality' I'm talking mainly about the randomness or distribution of the numbers output by the generator over the possible range of numbers that it can generate. Although, to a lesser extent I am also referring to period.

As for the modulo operator comment, I once read a paper (which I am currently trying to find) that discouraged the use of the modulo operator in combination with random number generators that suffer from poor uniformity coupled with a limited output range (such as some implementations of rand()).

I myself have experienced this problem, expecially when the range of numbers required is very small. You typically see that one or two values tend to be generated with a much greater frequency.

Hope thats clearer,
arm.

This topic is closed to new replies.

Advertisement