Jump to content
  • Advertisement

Archived

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

NnN

Generating a random number,

This topic is 5860 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

Hi, What is the best way to generate a random number so it wouldn''t be the same everytime I run the program. (I''m using MS visual C++ 6.0) When I try this: #include . . . srand(time(NULL)); i=rand()%(10); . . It works fine except it is really slow!!! And randomize(); Wouldn''t work on my compiler. So is there a better way? ####################### How do I fix my program so it would display something for a specific amount of time and then continue the program? I''m trying to show the user something and after s/he choose somthing I want to display what s/he choosed for few seconds then continue to display other stuff. Thanks!!

Share this post


Link to post
Share on other sites
Advertisement
What kind of computer do you have? Rand was pretty slow on my 8088 but it not''s that slow on most modern Pentiums I own by any means. A few hundred clock cycles at the most.

Yes use srand() like you have to seed the random generator.

you can make a little macro like this:

#define FRAND() ( (float)rand() / (float) RAND_MAX )

that''ll give you a float between 0..1


#define FRANDMM(min,max) ((max - min) * FRAND() + min)

to scale it between max and min


randomize is not available on Linux or GNU Compilers as far as I know. Borland has a randomize function. randomize() is used with the random() function, which is another generator.


There are literally tons of random number generators go search the net you''ll find one. Many good books have chapters on random number generation as it''s a very crucial aspect to simulations. Also there is quite a bit in the Game Gems books on random numbers too.


Share this post


Link to post
Share on other sites
What kind of computer do you have? Rand was pretty slow on my 8088 but it not''s that slow on most modern Pentiums I own by any means.

srand(...) seeds the random number generator. By using the unix clock you can guarantee to always generate a unique series until it folds over on 32-bit systems (another 60 years or so).
What you are doing is ok as it is.

Using the same seed with srand will always generate the exact same series as you most likely know.


You can make a little macro like this:

#define FRAND() ( (float)rand() / (float) RAND_MAX )

that''ll give you a float between 0..1


#define FRANDMM(min,max) ((max - min) * FRAND() + min)

to scale it between max and min


randomize is not available on Linux or GNU Compilers as far as I know. Borland has a randomize function. randomize() is used with the random() function, which is another generator but I only rememeber it from DOS.

I have no idea why it is slow for you though. I have executed rand() in loops many thousands of times and the whole results were less than a second on my Pentium 4. At the most it took about 1.7 seconds to execute a 1024*1024 loop with rand() on a pentium pro 200 which was also storing into an array in memory (a large array bigger than the cache on that system) but that was the total amount of time 1024*1024 times! Yes I did actual timings with this not to long ago I wanted to see how the P4 performed to the older systems I have. All in all it is more like a 1.6ghz Ppro than a 2.0ghz Ppro.

G''luck





Share this post


Link to post
Share on other sites
Are you calling srand() every time you call rand() as well? If so, that is not correct, you should only call srand() once in your entire program.

However if that is still not good enough check out the boost RNG library at www.boost.org.

Share this post


Link to post
Share on other sites
quote:
Original post by Premandrake
Are you calling srand() every time you call rand() as well? If so, that is not correct, you should only call srand() once in your entire program.


I figured that out after missing with the code a little but thanks everyone!

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!