Created
August 12, 2009 11:29
-
-
Save mumumu/166454 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <math.h> | |
#include <time.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <errno.h> | |
// | |
// returns random number from zero to max. | |
// to compile this, run the following command. | |
// $ gcc -Wall -lm -lrt filename | |
// | |
double getRand(int max) | |
{ | |
struct drand48_data data; | |
struct timespec time; | |
double rand = 0; | |
int result = clock_gettime(CLOCK_REALTIME, &time); | |
if (result == -1) { | |
perror("clock_gettime"); | |
return -1; | |
} | |
srand48_r(time.tv_nsec, &data); | |
drand48_r(&data, &rand); | |
rand = floor(rand * max); | |
return rand; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
int c; | |
for (c = 0; c < 100; c++) { | |
printf("%.0f\n", getRand(500)); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment