c++ - determinisitic random number generator giving different random number for same seed -
i want use deterministic random bit generator application. i' m using openssl random number generator apis. i'm using rand_pseuso_bytes() api generating pseudo random numbers. , i'm giving seed through rand_add(). if called random generator function 2 times, i'm getting 2 different random values @ these 2 calls. if seed same, should give me same values, have gone wrong ?
the code have written is
int nsize = 8; /* 64 bit random number required */ int nentropy = 5; /* 40 bit entropy required */ /* generate random nonce making seed */ rand_bytes(cseed_64, nsize); rand_add(cseed_64, nsize, nentropy); /* random nonce cseed_64, seedin 64 bit * 40 bit entropy */ /* calling random byte function generate random number function 10 times same seed*/ int j = 10; while( j--) { rand_pseudo_bytes(crandbytes_64, 8); printf("generated 64 bit random number \n"); for(i = 0 ; < nsize; i++) printf("%x ",crandbytes_64[i]); printf("\n"); }
but you're not calling rand_psuedo_bytes()
same seed, you're making successive calls it, should produce different outputs. that's whole point of "generator" function--it produces different value on each call based on internal state.
when "seed" , random number generator, fix internal state, after generate random numbers evolving state. each seed, generate unique , reproducible sequence of numbers repeated calls, won't generate same numbers on each call, pointless.
the line:
rand_bytes(cseed_64, nsize);
creates random see value based on system entropy. should check errors here, may fail if not enough entropy available.
the line
rand_add(cseed_64, nsize, nentropy);
does not seed prng, adds seed existing prng state. if want set prng state fixed value, have use rand_seed()
. if call rand_seed()
given value, rand_pseudo_bytes()
thereafter generate given sequence of random numbers. if call rand_seed()
again same value, repeat same sequence.
Comments
Post a Comment