#### Archived

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

# Random Number

This topic is 6273 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
Rutin
16
4. 4
5. 5

• 10
• 10
• 14
• 10
• 25
• ### Forum Statistics

• Total Topics
632647
• Total Posts
3007640
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

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!