ch7-frand (Float rand)
Chapter_7 Exercise_7-8 system | Exercise_7-9 |
frand.c K&R, p. 168 download
#include <stdio.h> // for printf()
#include <stdlib.h> // for rand(), srand(), RAND_MAX
#include <time.h> // for time_t, time()
#include <limits.h> // for INT_MAX
double frand(void); // floating point rand()
int main()
{
printf("RAND_MAX: %d\n", RAND_MAX);
printf("INT_MAX: %d\n", INT_MAX);
time_t t;
srand((unsigned) time(&t)); // seed rand()
printf("frand(): %lf\n", frand());
return 0;
}
double frand(void) // floating point rand()
{
return (double) rand() / (RAND_MAX + 1.0);
}
/*
gcc frand.c -o frand
./frand
RAND_MAX: 2147483647
INT_MAX: 2147483647
frand(): 0.326507
*/
Chapter_7 Exercise_7-8 system | BACK_TO_TOP | Exercise_7-9 |
Comments
Post a Comment