Find out which random number generator was seeded in C ++

I have an unmanaged C ++ console application in which I use srand () and rand (). I don't need this to solve a specific problem, but it was curious: the original seed is passed to srand () somewhere in the memory, which can I request? Is there any way to find out what a seed is?

+5
source share
4 answers

The seed does not need to be stored; only the last random number is returned.

Here is an example from manpage:

       static unsigned long next = 1;

       /* RAND_MAX assumed to be 32767 */
       int myrand(void) {
           next = next * 1103515245 + 12345;
           return((unsigned)(next/65536) % 32768);
       }

       void mysrand(unsigned seed) {
           next = seed;
       }
+7
source

If you have a simple linear congruent generator for which you have several values, this gives a system of equations:

 v1 = ( seed * a + b ) % m
 v2 = (   v1 * a + b ) % m;
 v3 = (   v2 * a + b ) % m;
... 

, :

seed = (v1 - b)/a (mod m)

, mod m ( , (0 < seed < m) ). v1 - b , m, .

, .

+3

, - , () ..

, ( ) , , , - .

0

, , / , . - rand() . , .

- , srand(), .

srand. , - (, , ) , , , (, , - , ). , - , "0" .

0

All Articles