#### Archived

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

# Random Number

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

## 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 on other sites
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 on other sites
Thanks, that did the trick!

##### 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

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 11
• 21
• 12
• 11
• 41
• ### Forum Statistics

• Total Topics
631401
• Total Posts
2999870
×