[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
int main (int argc, char *argv[])
{
FILE *urandom;
unsigned int seed;
int i;
urandom = fopen ("/dev/urandom", "r");
if (urandom == NULL) {
fprintf (stderr, "Cannot open /dev/urandom!\n");
exit (EXIT_FAILURE);
}
for (i = 0; i < 10; i++) {
fread (&seed, sizeof (seed), 1, urandom);
srand (seed);
printf ("Random number from 1 to 100: %d\n", (int) floor (rand() * 100.0 / ((double) RAND_MAX + 1) )+ 1);
}
return 0;
}