• Advertisement

Archived

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

Random Number

This topic is 6057 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 am trying to move an object across the screen with a random velocity. How can I generate a small random number? Using the following function gives numbers that are way to big! srand( (unsigned)time( NULL ) ); Preferably is there a way I can make an array to store several velocities and pick one at random? The velocities are on the order of something like .002, .003. etc.. If there is a better way to generate rand numbs, please tell me. I remember one time how I generated randrom numbers from a range, but I can''t remember the syntax. Thanks, chris

Share this post


Link to post
Share on other sites
Advertisement
hey,there is a windows random function in the windows.h or windowsx.h(i cant remember which one)but it is rand()%16.
or in game terms

x_velocity=-8 + rand()%16

hope that helps

.............................

Share this post


Link to post
Share on other sites
Put the following once at the beggining of your program:
srand((unsigned)time(NULL));  

That "initializes" the random number generator to the number of seconds since 1970, so that your program will produce a different set of random numbers every time it is run (otherwise, you'll just keep getting the same pattern of numbers with each run). Then, to actually get a random number, use this:
rand() 

You'll have to mod (%) to get the number in the range you want.

- Fuzz

Edited by - Holy Fuzz on July 23, 2001 12:22:19 PM

Share this post


Link to post
Share on other sites

  • Advertisement