Deterministic random number generator giving a different random number for the same seed


I want to use a deterministic random bit generator for my application. I' m using openssl for random number generator apis. Currently I'm using RAND_pseuso_bytes() api for generating pseudo random numbers. And I'm giving seed through RAND_add(). Then if I called the Random generator function two times, I'm getting two different random values at these two calls. If the seed is same, then it should give me the same values, where I have gone wrong ?

The code I have written is

int nSize = 8;    /* 64 bit random number is required */
int nEntropy = 5; /* 40 bit entropy required */
/* generate random nonce for making seed */
RAND_bytes(cSeed_64, nSize);
RAND_add(cSeed_64, nSize, nEntropy); /* random nonce is cSeed_64, seedin 64 bit with
                                      *  40 bit entropy */
/* calling random byte function to generate random number function 10 times with same seed*/
int j = 10;
while( j--)
    RAND_pseudo_bytes(cRandBytes_64, 8);
    printf("generated 64 bit random number \n");
    for(i = 0 ; i < nSize; i++)
        printf("%x ",cRandBytes_64[i]);

But you're NOT calling RAND_psuedo_bytes() with the same seed, you're making successive calls to it, which should produce different outputs. That's the whole point of a "generator" function--it produces a different value on each call based on internal state.

When you "seed" and random number generator, you fix its internal state, after which it will generate random numbers by evolving that state. For each seed, it will generate a unique and reproducible sequence of numbers from repeated calls, but it certainly won't generate the same numbers on each call, that would be pointless.

The line:

RAND_bytes(cSeed_64, nSize);

creates a random see value based on system entropy. You really should check for errors here, as it may fail if not enough entropy is available.

The line

RAND_add(cSeed_64, nSize, nEntropy);

DOES NOT seed the PRNG, it adds the seed to the existing PRNG state. If you want to set the PRNG state to a fixed value, you have to use RAND_seed(). If you call RAND_seed() with a given value, RAND_pseudo_bytes() will thereafter generate a given sequence of random numbers. If you call RAND_seed() again with the same value, it will then repeat the same sequence.