thanks to Michael Ashley...
Code:// An example showing how to generate random floating // point numbers between 0 and 1. // Michael Ashley / UNSW / 02-Apr-2003 #include <sys/time.h> #include <stdio.h> #include <stdlib.h> #include <assert.h> int main(void) { struct timeval t; // Note that the structure timeval is defined in <sys/time.h> as {int t.tv_sec int t.tv_usec} // Here t.tv_sec is the time in seconds and // t.tv_usec is the fractional part of the time in microseconds int i; // Obtain the time of day, to microsecond resolution. assert(0 == gettimeofday(&t, NULL)); printf("The time is %ld.%06ld seconds since the Unix epoch\n", t.tv_sec, t.tv_usec); // Use the number of microseconds as the seed for the system // random number generator. srandom(t.tv_usec); // Generate and print ten random integers between 0 and RAND_MAX. for (i = 0; i < 10; i++) { printf("%ld\n", random()); } // Generate and print ten random numbers between 0 and 1.0. for (i = 0; i < 10; i++) { printf("%.15f\n", random()/((double) RAND_MAX)); } return 0; }



LinkBack URL
About LinkBacks


