| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Random number.
Hi,
I am writing a program and I just need a little help. I need my program to generate a random number. Does anyone know how to do this? If so please let me know. Thanks. -nCmdShow |
|
#2
|
|||
|
|||
|
use these functions:
long random(void); void srandom(unsigned long seed); example: int i; srandom(time(0)); for (i = 0; i < 10; i++) printf("%u\n", random()); -- julien barbier |
|
#3
|
|||
|
|||
|
or you could use:
srand(GetTickCount()); //initialises the function each time with the computer tick count so it is a garenteed random number int random = rand()% 6; //the 6 defines that it will give you a number between 0 and 6, you can change it to whatever you like -drizzle |
|
#4
|
|||
|
|||
|
Thanks alot for all your help.
|
|
#5
|
|||
|
|||
|
To use a random number you use rand (); to generate a random number. Everytime you run the program, you will receive the same sequence of numbers, but if you seed the random number with ( time (0)), this will like the random generator to the internal clock. Be sure to include the <ctime> header file. Also, only call the srand function once in your program. Calling it more than once can give you problems.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Random number. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|