#include <stdlib.h>
#include <time.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
// for random numbers of 1 to RANGE
#define RANGE 1000
int main(void)
{
int rand_num;
int fd;
int i;
int which;
int check[RANGE];
int done = 0;
int iters = 0;
for (i = 0; i < RANGE; i++)
{ check[i] = 0;
}
fd = open("/dev/urandom", O_RDONLY);
if (fd != -1)
{ read(fd, &rand_num, 4);
}
else
{ printf("can't read from /dev/urandom!\n");
return 1;
}
srandom(rand_num); // set the seed value
while(!done)
{ iters ++;
which = 1 + (int) ((RANGE + 0.0)*random()/(RAND_MAX + 1.0));
printf("iter: %d rand: %d\n", iters, which);
check[which - 1] = 1;
done = 1;
for (i = 0; i < RANGE; i++)
{ if (check[i] == 0)
{ done = 0;
} } }
printf("took %d iterations to find all values\n", iters);
return 0;
}
--
MattWalsh - 29 Nov 2004