Skip to main content
GameDev.net gamedev.net
🔒 Locked

Random number? Help Please!

Started by Gameman17 Nov 27, 2001 at 11:54 AM 4 replies 350+ views
Original Post
Gameman17
Gameman17
Please help, I am using am writing a text game and I need a random number generator. I have been trying to write my own and I have also tried downloading one on the internet, but they all give the same list of random numbers every time I execute my c++ program! I need it to be different each time I play the game not the same numbers over and over! Please someone help!
randomDecay
randomDecay
You need to seed the random number generator at the top of your main function like so:

      
#include <time.h>



//in your main function

srand(time(NULL)); // seed the randomizer



You have to also include the time include from this example above your functions.



Edited by - randomDecay on November 27, 2001 1:01:07 PM
Gameman17
Gameman17
Could you give me an example of this I am just learning c++, this text game I ame writing is my first using c++. I used to write in basic!
Martee
Martee
int main(int argc, char* argv[])
{
srand((unsigned int) time(0));
int i = rand() % 100; // random number between 0 and 99
int j = rand() % 5; // random number between 0 and 4
return 0;
}
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Gameman17
Gameman17
That was just what I needed! Thankyou!!!!!!!!
merlin9x9
merlin9x9
Oh, I remember my BASIC days. =) Okay, so here we go. In BASIC (QuickBASIC, anyway), you use RANDOMIZE TIMER to tell the computer to seed the random number generator with the current time. Since any random number generator is an algorithm, it will produce the same sequence when started with the same seed. That''s why often the timer is used since, theoretically, the timer will hold a different value each time we seed the generator.

The random number stuff mentioned in other replies come from C, the language upon which C++ was based. In C, there''s no single statement we can use to do the random stuff, so we need to do it in the 2 steps it actually is. The srand function (make sure you include time.h) takes an unsigned integer that represents the seed value to use for the generator. To get the current time there''s the time function. It takes a pointer to a certain structure, but we don''t care about getting extended information back, so we can just pass it NULL, which effectively is a "nothing" value for a parameter that requires something (assume that no function requiring pointer arguments can accept this unless you see otherwise in the documentation). With that argument, it returns the current tick. So, we just feed that to the srand function as randomDecay and Martee pointed out. And make sure you only do this one time in your program, too, just like you would in BASIC.

The next bit is getting random values from the generator. If I''m not mistaken, BASIC''s RND function returned a fractional value from 0.0 to 1.0. Normally, you''d multiply that by the range you wanted, add that result to the lowest number in your range, then convert that to the data type you needed (such as in low_val% + INT(RND * (high_val% - low_val%))). C''s analogous function, rand returns an integral value from 0 to RAND_MAX, a constant defined in stdlib.h. Now, Martee''s example uses modulus arithmetic to return a value within a desired range. Unfortunately, that trick somewhat ruins the randomness of the numbers themselves because of the way they''re represented as bits. The most accurate way (and closest to BASIC, according to my recollection), would be to divide the value rand gives you by RAND_MAX to give you a 0.0-to-1.0 value which you''d then muliply by your desired range. But since C/C++ only perform floating-point division when there''s at least 1 floating-point value to be divided, we''ll either have to convert rand''s return value to floating-point, or do the same for RAND_MAX; then after multiplying that by the range, we''ll have to convert that to the data type we want, as done in BASIC. Let me give you a function that returns an integer from low to high (for demonstration purposes):

  
int irand(int low, int high)
{
return low + (int)((high - low) * (rand() / (float)RAND_MAX));
}


Note that this function assumes that the srand bit has already been done. And don''t forget either to have the function body appear before you use it in the code (like if you use it in main, make sure it''s body is above main''s body, otherwise you''ll have to make a prototype: int irand(int low, int high);

Anyway, let''s say you want a random number from -5 to 200. Just call irand(-5, 200). Not too bad, eh?

Good luck.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.