Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

bioagentX

Random Number Help

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
const float FRandMax = RAND_MAX;

#define RND (rand()/FRandMax)

cout << RND*100;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!