Random Number

Started by
2 comments, last by nanobyte 22 years, 8 months ago
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
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

.............................
.............................
Thanks, that did the trick!
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

This topic is closed to new replies.

Advertisement