Random Number Help

Started by
7 comments, last by bioagentX 21 years ago
I was wondering if anyone knew the syntax for generating random numbers. I know you have to use a certain function to obtain random numbers but I don't know what function it is. Is there are certain library you need to use or object that you need to include. I was thinking that the code would look somewhat like


cout<< random(100);

  
. Where the random function above would generate a random number between 0 and 99. Anyway, if someone could please help me I would be really grateful. -BioagentX P.S. I am using the Dev C++ compiler, I don't know if that has anything to do with it, but I felt like I should say it. [edited by - bioagentX on March 30, 2003 9:08:03 PM]
There are three types of people in this world, those who can count, and those who can't
Advertisement
const float FRandMax = RAND_MAX;

#define RND (rand()/FRandMax)

cout << RND*100;
Don''t forget to seed your random function. If you don''t seed the function and keep calling it, it will give you the same result every time. rand() has a starting number that is always the same and then it generates another number using a formula. Your job to create a truely random number is to seed it with a new starting number each time. You want to use the srand() function for that. Usually you want to convert the time to a new number because it''s different enough each time.

- Halcyon

I don''t know the details of how this function works, but maybe someone else could help you out more.
"Computers in the future may weigh no more than 1.5 tons."- Popular Mechanics, 1949
The above poster (HalcyonBlaze) is partially incorrect. You only need to seed the random number generation ONCE when your program starts up. If you don''t, it will generate the same sequence each time you run your program (which can be desirable for debugging purposes). The function you use to seed the random number generator is srand, and it is usually called with the time:

srand((unsigned)time(NULL));


[edited by - Big Brother on January 1, 1984 12:00:00 AM]
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Thanks, a lot for your help guys, I really appreciate it.
There are three types of people in this world, those who can count, and those who can't
At the top of the main program to set the seed equal to current time:
srand(time(0));

After you set the seed for the random number generator at the beginning of your program, like Extrarius said, I just use this formula to generate numbers:

number = B + rand() % R;

number is the random number generated,
B is the base,
R is the range.

rand() % 100 will give a number between 0 and 99. (Range 100, base 0)

1 + rand() % 6 will give a number between 1 and 6. (Range 6, base 1, a 6 sided die)

You can use more than 1 of these in the same line, so if we wanted to roll 3 6 sided dice and add the results, we''d use:

number = 3 + rand() % 6 + rand() % 6 + rand() % 6;

Don''t forget the includes:
#include <cstdlib>
#include <ctime>

(silencer)
I have also found it useful to only randomize the seed on the release version and not the debug version. Makes it easy to get some bugs which are dependent on a sequence of pseudorandom events to repeat, which makes them easier to debug.
.
For debug versions, I''ll test with various seeds. Some bugs are more appearant with some seeds more than others.

Test specific seeds using

srand(x);

x is a positive integer of the seed you''re using.

(silencer)
I probably worded it funny, but i meant that the programs has to seed the randomn function with a new number evertime the application starts up (So one call, but the number going in has to be diff each time the app starts).

- Halcyon

Thanks for clarifying/correcting me.
"Computers in the future may weigh no more than 1.5 tons."- Popular Mechanics, 1949

This topic is closed to new replies.

Advertisement