Re: [Dev-C++] How do you get random numbers in devc++?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: David M. <ci...@ya...> - 2002-03-29 15:06:48
|
First you need to include stdlib.h next you need to call the function srand() to initialize the random number generator If the number you use is the same the same sequence of random numbers is generated, because of this it is normal to use the time function to initialize the generator as the time is constantly changing e.g. srand(time(NULL)); N.B. You need to include time.h to get the time function. next you want to call the rand function to get your random number N.B. the rand function returns a random number between 0 and RAND_MAX usually you wish a number within a certain range the following formula should help you. lower_bound + rand() % (upper_bound - lower_bound + 1) eg. To get a random number between 1 and 10 (inclusive) the following should sufice 1 + rand() % (10 - 1 + 1); |