c++ - Linear congruential generator: How important is setting seed? -
i'm learning linear congruential generator in algorithms , data structures course. after thinking rng implementation we've been using (a=429493445, c=907633385, mod=4294967296, x _uint32), 1 thing came mind: program has function setting seed.
how important function in c , c++?
here's line of thought: once program starts, os assigns addresses used variables. data in memory location given seed can interpreted number.
i understand in small computers may happen operating system (if there one) assigns several times same address seed wouldn't data contained in address different every time? unless system sets free ram value after every start, data contained in ram pretty random , provide enough seed.
even if data contained in space given seed used program, don't see how make impact on generator itself.
while unserious playing around rngs, seeding "junk in memory" work ok. bad way things: have no guarantee data in memory is random, though can be. in security applications (which guess not relevant here, since you're using linear congruence generator) don't want parts of environment not under control (ram) act seed. in other applications, such scientific computing, want results reproducible. let user choose seed himself, , provide option sample os rng seed (/dev/random on unix-like systems).
the bottom line is: if want random seed, source of randomness. pretty accessible source provided os better "hey, what's in ram @ location today?". sampling example /dev/urandom no more difficult sampling ram, , it's right thing (for non-security-sensitive things, of course).
edit: here's how sample /dev/urandom on gnu/linux using c:
int seed; file* urandom = fopen("/dev/urandom", "r"); fread(&seed, sizeof(int), 1, urandom); fclose(urandom);
now seed
holds integer ready used seed prng (you may need different datatype).
wikipedia has some nice information on /dev/random , /dev/urandom.
Comments
Post a Comment