random number?

Started by
11 comments, last by lmelior 15 years, 5 months ago
How can I get a random number, specificly a random number between two numbers in C++? I've learnt you need the rand() or rand_s() function(The latter is better isnt it?) though I don't really know how to work with it even less how to get a random number between two numbers with it... If anyone could help me out with this I'd be grateful. Thanks.
Advertisement
Why don't you try Googling and reading the material on random number generation in C++?

Also, to have a random number between a set range, you need to do a few mathematical operations. It's not complicated if you think about it. Or look it up online to see the basic formulas.
Here is the pseudocode. it will look something like this:
if (rand() % 2 == 0){   // do this}else{   // do that}
www.dannylum.com/games_projects.html - My Game Projectswww.dannylum.com/D2DProject - My Open Source 2D Map Editor
I usually do something like this
srand((unsigned)time(0));      randomNumber1 = (rand()%10)+1;
for random numbers between 1 and 10 but like oler1s said just google it and you get quite a few good reads on them
Use the modulo and then offset the outcome, such as:
Let's say you want a "random" number between 13 and 78.

srand(time(0)); // initialize randomizer

int r = rand(); // pick a random number
r = r % 66; // limit that number to between 0 and 65
r += 13; // this will offset it by 13, giving you some number between 13 and 78
cause noone said it yet.
call srand(time(0)); ONCE in you code when you want to initalize the random number sequence (ie the top of main)

then call rand() to get your random numbers.
You can also get a good random distribution between two numbers with:

float range = highNum - lowNum;
float num = ((float)rand() / (float)RAND_MAX) * range + lowNum;
I've gone ahead and looked into the subject a bit, so rand() isn't the best method to get random numbers? Will this method give me problems in the long run? I require random numbers for simple stuff such as a damage range for attacks, a probability to hit target etc..
Quote:Original post by Antonym
I've gone ahead and looked into the subject a bit, so rand() isn't the best method to get random numbers? Will this method give me problems in the long run? I require random numbers for simple stuff such as a damage range for attacks, a probability to hit target etc..


for a thing like damage or probability you're a better shot with computing it based on some data rather than random'ing, random() isn't that much random as you think. Set srand(time(0)) as I said before, put it at the beginning of your main or whatever so it initializes the randomizer based on current time, this will give you less chance on the numbers repeating.
Note that using modulus (%) can have bad consequences based on the underlying implementation of rand(). Many implementations use an LCG or other similar type of pseudo-random generator, which may result in a low order of "randomness" in the lower bits of the value, so that naively taking the modulus of the result can be bad. (For instance, many LCG implementations will return alternately even/odd results, so that the final results of a mod operation will also alternate even and odd). Even higher order PRNGs frequently exhibit a low amount of randomness in the lower order bits.

A better way is to normalize the result before scaling to a range. rand() returns a value in the range [0..RAND_MAX), so casting the result to a double and dividing by RAND_MAX+1 returns a number in the range [0,1) (ie, approaching 1 but never actually reaching 1) which you can then scale and truncate into any desired integral range, [Low, High]:

double d=(double)rand() / ((double)RAND_MAX + 1.0);
int roll=(int)(Low + d*(High-Low));

You can also bit-shift the result of rand() a few places before taking the modulus, although this can reduce the period of the sequence, or amount of time before the values repeat themselves.
Quote:Original post by Antonym
I've learnt you need the rand() or rand_s() function(The latter is better isnt it?)


Not really; it's Microsoft-specific.

There's nothing magical about any of the formulas you're being given here. The function gives you a random number across a specific range, and you do math in order to map that into the range that you want. If you Think(TM), you can figure these kinds of things out for yourself.

This topic is closed to new replies.

Advertisement