Created
September 25, 2013 15:15
-
-
Save carlcarl/6701147 to your computer and use it in GitHub Desktop.
random with /dev/urandom
This file contains hidden or 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 <stdio.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <math.h> | |
int main (int argc, char *argv[]) | |
{ | |
FILE *urandom; | |
unsigned int seed; | |
urandom = fopen ("/dev/urandom", "r"); | |
if (urandom == NULL) { | |
fprintf (stderr, "Cannot open /dev/urandom!\n"); | |
exit (1); | |
} | |
fread (&seed, sizeof (seed), 1, urandom); | |
srand (seed); /* seed the pseudo-random number generator */ | |
printf ("Random number from 1 to 100: %d\n", (int) floor (rand() * 100.0 / ((double) RAND_MAX + 1) )+ 1); | |
exit (0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment