C/C++中随机数生成器的实现 [英] implementation of the random number generator in C/C++

查看:113
本文介绍了C/C++中随机数生成器的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C中的随机数生成器的实现有点困惑,这也与C++中的明显不同

如果我理解正确,对 'srand(seed)' 的调用会以某种方式初始化一个可由 'rand()' 访问的隐藏变量(种子),后者又将该函数指向一个预先生成的序列,例如对于

为了完整性,标准引用:

<块引用>

7.22.2.1 rand 函数

rand 函数计算范围为 0 到RAND_MAX.
rand 函数不需要避免与其他伪随机调用的数据竞争序列生成函数.实现的行为就像没有库函数一样调用 rand 函数.
[...]
RAND_MAX 宏的值应至少为 32767.

7.22.2.2 srand 函数

srand 函数使用参数作为新的伪随机序列的种子由后续调用 rand 返回的数字.如果 srand 被调用相同的种子值,伪随机数序列应重复.如果 rand 是在对 srand 进行任何调用之前调用,应生成相同的序列就像 srand 第一次以种子值为 1 调用时一样.
srand 函数不需要避免与其他伪随机调用的数据竞争序列生成函数.实现的行为就像没有库函数一样调用 srand 函数.

C++ 包括 randsrandRAND_MAX,没有通过引用 C 标准进行更改.
不过,有一些 C++ 库函数/类被明确记录为使用 C 随机数生成器.

I am a bit confused by the implementation of the random number generator in C, which is also apparently different from that in C++

If I understand correctly, a call to 'srand(seed)' somehow initializes a hidden variable (the seed) that is accessible by 'rand()', which in turn points the function to a pre-generated sequence, like for example this one. Each successive call to 'rand()' advances the sequence (and apparently there are other ways to advance in C++), which also suggests the use of an internal hidden pointer or counter to keep track of the advance.

I have found many discussions on how the algorithms for pseudo-random number generation work and the documentation of the functions rand() and srand(), but haven't been able to find information about these hidden parameters and their behavior, except for the fact that according to this source, they are not thread-safe.

  1. Could anybody here please shed some light as to how are these parameters defined and what should be their defined behavior according to the standards, or if their behavior is implementation-defined?

  2. Are they expected to be local to the function/method that calls rand() and srand()? If so, is there a way to communicate them to another function/method?

If your answer is specific to either C or C++, please be so kind to point it out. Any information will be much appreciated. Please bear in mind that this question is not about the predictability of data generated by rand() and srand(), but about the requirements, status and functioning of their internal variables as well as their accessibility and scope.

解决方案

The requirements on rand are:

  • Generates pseudo-random numbers.
  • Range is 0 to RAND_MAX (minimum of 32767).
  • The seed set by srand() determines the sequence of pseudo-random numbers returned.
  • It need not be thread-safe or even reentrant, the state can be stored in a static variable.

The standard does not define any way to recover the internal state for reseeding or anything else.

There is no requirement on what PRNG is implemented, so every implementation can have its own, though Linear Congrueantial Generators are a favorite.

A conforming (though arguably useless) implementation is presented in this dilbert strip:

http://dilbert.com/strips/comic/2001-10-25/

Or for those who like XKCD (It's a perfect drop-in for any C or C++ library ;-)):

For completeness, the standard quotes:

7.22.2.1 The rand function

The rand function computes a sequence of pseudo-random integers in the range 0 to RAND_MAX.
The rand function is not required to avoid data races with other calls to pseudo-random sequence generation functions. The implementation shall behave as if no library function calls the rand function.
[...]
The value of the RAND_MAX macro shall be at least 32767.

7.22.2.2 The srand function

The srand function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand. If srand is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated. If rand is called before any calls to srand have been made, the same sequence shall be generated as when srand is first called with a seed value of 1.
The srand function is not required to avoid data races with other calls to pseudo-random sequence generation functions. The implementation shall behave as if no library function calls the srand function.

C++ includes rand, srand and RAND_MAX without change by reference from the C standard.
There are a few C++ library functions/classes which are explicitly documented to use the C random number generator though.

这篇关于C/C++中随机数生成器的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆