void init_random () { unsigned int ticks; struct timeval tv; int fd; gettimeofday (&tv, NULL); ticks = tv.tv_sec + tv.tv_usec; fd = open ("/dev/urandom", O_RDONLY); if (fd > 0) { unsigned int r; int i; for (i = 0; i < 512; i++) { read (fd, &r, sizeof (r)); ticks += r; } close (fd); }
srand (ticks); printf("init finished "); }
unsigned int new_rand () { int fd; unsigned int n = 0; fd = open ("/dev/urandom", O_RDONLY); if (fd > 0) { read (fd, &n, sizeof (n)); } close (fd); return n; }
int main () { int n, i; init_random (); n = rand (); printf ("n=%d ", n); for(i=0;i<10;i++) printf ("%u ", new_rand()); }