rand()/srand()函数是如何用C实现的 [英] How is the rand()/srand() function implemented in C

查看:54
本文介绍了rand()/srand()函数是如何用C实现的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
rand() 是如何工作的?它有某些倾向吗?有什么更好用的吗?

我知道如何实现它.但是,我想了解 rand 在内部的行为方式以及为什么需要为 rand 函数初始化种子"值.

I know how to implement it. However, I would like to understand how the rand behaves internally and why is it necessary to initialise a 'seed' value for the rand function.

或者说——rand 函数如何使用种子值来生成随机数?

Alternately put — how does the rand function use the seed value to generate random numbers?

推荐答案

具体的实现细节取决于实现者.但是 GNU 实现 (glibc) 是这样实现 rand() 的:http://sourceware.org/git/?p=glibc.git;a=blob;f=stdlib/random_r.c;hb=glibc-2.15#l361

The exact implementation details are up to the implementors. But the GNU implementation (glibc) implements rand() like that: http://sourceware.org/git/?p=glibc.git;a=blob;f=stdlib/random_r.c;hb=glibc-2.15#l361

评论很好地解释了这一点.

The comment explains it pretty well.

/* If we are using the trivial TYPE_0 R.N.G., just do the old linear
    congruential bit.  Otherwise, we do our fancy trinomial stuff,
    which is the same in all the other cases due to all the global
    variables that have been set up.  The basic operation is to add
    the number at the rear pointer into the one at the front
    pointer.  Then both pointers are advanced to the next location
    cyclically in the table.  The value returned is the sum generated, 
    reduced to 31 bits by throwing away the "least random" low bit.    
    Note: The code takes advantage of the fact that both the front and 
    rear pointers can't wrap on the same call by not testing the rear  
    pointer if the front one has wrapped.  Returns a 31-bit random number. */

关于您为什么总是需要种子值的问题:在计算机科学中没有真正的随机数.计算机(在计算理论中)是完全确定性的机器.他们不能执行任何操作的结果是随机的.

Regarding your question why you always need a seed value: There are no truly random numbers in computer science. Computers are (in computation theory) completely deterministic machines. They can't perform any operations with an outcome which is up to chance.

只有伪随机数生成器可以生成看起来随机的数字流,但它们仍然是确定性计算的结果.这就是您需要种子值的原因:每个种子产生不同的数字序列.当你使用相同的种子时,你会得到相同的伪随机数序列.

There are only pseudorandom number generators which generate streams of numbers which look random, but they are still the results of deterministic calculations. That's why you need a seed value: each seed results in a different sequence of numbers. When you use the same seed, you get the same sequence of pseudorandom numbers.

可以利用RNG在获得相同种子时总是返回相同序列的行为:经典空间模拟Elite 能够将一个包含数百颗行星的巨大宇宙存储在一个整数中.它是怎么做到的?整个宇宙都是随机产生的.重建宇宙所需的所有数据都是种子值,它总是导致生成完全相同的宇宙.

The behavior that a RNG always returns the same sequence when getting the same seed can be exploited: The classic space simulation Elite, for example, was able to store a huge universe with hundreds of planets in a single integer. How did it do that? The whole universe was randomly-generated. All data which was required to recreate the universe was the seed value, which always resulted in the exact same universe being generated.

这篇关于rand()/srand()函数是如何用C实现的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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