Randomness and reseeding?
Susan Margulies
smargulies at ucdavis.edu
Tue Dec 12 19:58:45 CET 2006
Hello, everyone! I am generating many, many numbers between 0 and 1. I
am using gmp_randclass and setting it to gmp_randinit_default, and
then seeding it with a read from /dev/random/ using some code gotten
off the web.
However, I began to notice that as I did more and more trials (like a
million), it seemed as if I was loosing the randomess. I was not
reseeding my random number generator... should I do that? If so, after
about how many trials? I noticed experimentally that if I reseeded too
often from /dev/random, the first time it reads 128 bytes, and the
rest of the times it only reads 8 bytes. Maybe it hasn't saved up
enough randomess between reads?
I also tried combinations of reading from /dev/random and reseeding
using the system time. Seeding from /dev/random defintely seems to
work better.
I can see that I really don't understand these random number
generators. Can someone advise me how to generate 2 million numbers
between 0 and 1 with a uniform distribution? How can I find a really
high quality seed?
Here is the code I pulled from the web:
void Graph::read_random(char *dat, int &bytes_read) {
//const char *dev = "/dev/urandom";
const char *dev = "/dev/random";
int i, fd;
fd = open(dev, O_RDONLY);
if (fd != -1 ) {
bytes_read = read(fd, dat, RAND_LEN);
if (bytes_read != RAND_LEN) {
printf("read_random: read %d bytes from %s\n", bytes_read,
dev);
}
for( i = 0; i < bytes_read; i++ ) {
dat[i] = (dat[i] & 0x07) + '0';
}
//printf("%s: %s\n", dev, dat );
} else {
printf("ERROR! Failed reading from %s\n", dev);
exit(1);
}
close( fd );
}
I don't know what he does the & here.... he seemed to say he needed to
format the numbers according the what gmp_rand_class needed.
Thank you very, very much for any input that you have.
Best,
Susan
More information about the gmp-discuss
mailing list