//============================================================================== // Illustration of how to initialize and use the built-in C random number // generator rand(). // // This version creates a "random" seed from the time of day and uses // this seed to initialize the random number generator by using it as an // argument to srand. The result of time(NULL) is the (integer) time in // seconds from some zero of time way back in the past. Hence, if you execute // the code twice within a second you will get the same string of random // numbers. // // Alternatively you could type in a specific value for the integer seed, i.e. // // int seed; // seed = 314159; // srand(seed); // // rand() gives an int between 0 and RAND_MAX where // RAND_MAX = 2147483647 = 2^31 - 1 // // We divide rand() by RAND_MAX + 1.0 to convert it to a float between 0 // and 1 (the "+ 1.0" is to force floating point division rather than integer // division). //============================================================================== #include #include #include main() { int k, N; double x; srand(time(NULL)); // Seed the random number generator with a "random" seed N = 20; for(k=0; k