简体   繁体   English

确定性随机数发生器为同一种子提供不同的随机数

[英]determinisitic random number generator giving different random number for same seed

I want to use a deterministic random bit generator for my application. 我想为我的应用程序使用确定性随机位生成器。 I' m using openssl for random number generator apis. 我正在将openssl用于随机数生成器api。 Currently I'm using RAND_pseuso_bytes() api for generating pseudo random numbers. 目前,我正在使用RAND_pseuso_bytes()API生成伪随机数。 And I'm giving seed through RAND_add(). 我正在通过RAND_add()提供种子。 Then if I called the Random generator function two times, I'm getting two different random values at these two calls. 然后,如果我两次调用Random generator函数,那么在这两次调用中将获得两个不同的随机值。 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]);
    printf("\n");
}

But you're NOT calling RAND_psuedo_bytes() with the same seed, you're making successive calls to it, which should produce different outputs. 但是,您不是用相同的种子调用RAND_psuedo_bytes() ,而是对其进行了连续调用,这产生不同的输出。 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. 不播种PRNG,而是种子添加到现有的PRNG状态。 If you want to set the PRNG state to a fixed value, you have to use RAND_seed() . 如果要将PRNG状态设置为固定值,则必须使用RAND_seed() If you call RAND_seed() with a given value, RAND_pseudo_bytes() will thereafter generate a given sequence of random numbers. 如果您使用给定值调用RAND_seed() ,则RAND_pseudo_bytes()随后将生成给定的随机数序列 If you call RAND_seed() again with the same value, it will then repeat the same sequence. 如果再次使用相同的值调用RAND_seed() ,它将重复相同的序列。

您可能希望在每次调用它时都将其作为种子,否则期望每次调用时都将返回一个随机数。

In your code 在你的代码中

RAND_bytes(cSeed_64, nSize);
RAND_add(cSeed_64, nSize, nEntropy);

You are generating a random number using RAND_bytes . 您正在使用RAND_bytes生成一个随机数。 You are adding this random number into seed. 您正在将此随机数添加到种子中。 Since, you are adding randomness to already something random, it should generate random number every time. 由于您是在已经随机的事物上添加随机性,因此每次都应生成随机数。 So, seed is not same everytime since it is random. 因此,种子是随机的,因此每次都不相同。

To keep the seed same, try RAND_seed with a fixed seed to get the expected behaviour. 要保持种子不变,请尝试使用固定种子的RAND_seed以获得预期的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM