Question about random seed?
Susan Margulies
smargulies at ucdavis.edu
Tue Dec 12 15:13:56 CET 2006
Hello! I confess I copied some code from the web without really
worrying about it too much. But now, I am not quite sure that it has
good random behavior.
Here is my code to set the seed:
char dat[RAND_LEN];
int bytes_read;
/* get random seed */
memset(dat, 0, RAND_LEN*sizeof(char));
Graph::read_random(dat, bytes_read);
mpz_class seed(dat);
gmp_randclass r(gmp_randinit_default);
r.seed(seed);
And here is my read_random:
/* read random bits from /dev/random/ for generating random graphs */
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);
//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 );
}
My RAND_LEN is 1024. On the web, the person seemed to say that he did
the & step to put the data in the format appropriate for the seed.
Does this seem reasonable / necessary?
I need about 40,000+ numbers uniformly distributed between 0 and 1. Am
I doing the right thing here? My code is behaving "strangely". When I
try to get a baseline of some probability tests, I found a 90% sucess
rate for a particular thing that I am calculating. But sometimes when
I try to calculate that result, I can do trial after trial after trial
without a success. If my rate really is 90%, that shouldn't happen...
which is why I thought that maybe I wasn't using the randomness quite
right.
I would love some advice! I read the documentation, but it didn't
quite answer my question.
Thank you very much for your time!
Best,
Susan
More information about the gmp-discuss
mailing list