Skip to content

Instantly share code, notes, and snippets.

@carlcarl
Created September 25, 2013 15:15
Show Gist options
  • Save carlcarl/6701147 to your computer and use it in GitHub Desktop.
Save carlcarl/6701147 to your computer and use it in GitHub Desktop.
random with /dev/urandom
#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