如何兰特()做出的随机数,即使你不随时间(NULL)种子呢? [英] How can rand() make random numbers, even when you don't seed it with time(NULL)?

查看:186
本文介绍了如何兰特()做出的随机数,即使你不随时间(NULL)种子呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白什么是传递到函数srand()作为参数将作为种子调用兰特()这样以后发生。一般时间(NULL)在过去。

但是如果你只叫函数srand 一次,然后你兰特正在不断地在循环中调用,怎么是数字各不相同?种子是从时间(NULL)返回的值 ...但只使用一个特定的返回值。因此,如何正在生成不同的号码?

我不每次使用线程和不同的种子了解线程在C.什么,但为函数srand ,以便后续调用到兰特其实并返回随机数?我注意到,当我通过一个静态的数函数srand 而不是时间(NULL)兰特仍将产生随机数。


解决方案

草案C99标准提供了部分样本实施 7.20.2.2 srand函数的,这将很可能清理它是如何为你工作:


  

实例下面的函数定义一个移植实现兰特和函数srand的。

 静态unsigned long int类型旁边= 1;
INT兰特(无效)// RAND_MAX假设为32767
{
    接下来=下一个* 1103515245 + 12345;
    收益率(无符号整数)(下一个/ 65536)%32768;
}无效函数srand(无符号整数种子)
{
    接下来=种子;
}


函数srand 设置的静态的变量接下来,然后设置为新值兰特,然后后续调用兰特将搭载与的previous价值接下来

例如,如果我们使用函数srand(1)然后顺序应该是这样的,因为我们做出兰特通话

 下一= 1
接下来= 1 * 1103515245 + 12345 = 1103527590
接下来= 1103527590 * 1103515245 + 12345 = 1217759518843121895
接下来= 1217759518843121895 * 1103515245 + 12345 = 15426401326682203284

由于HostileFork指出,数字不是真正的随机,但,它说:


  

也被称为确定性随机位产生器(DRBG),[1]是一种
  算法用于产生一个数字序列,其属性
  近似​​随机数序列的性质。该
  PRNG生成序列不是真正随机的,因为它是完全
  由相对小的组初始值决定的,称为
  PRNG的种子(其可以包括真正的随机值)。虽然
  真正随机的可使用产生更接近序列
  硬件随机数发生器,伪随机数发生器
  在实践中重要的是其在数产生和它们的速度
  重复性。


此特定的实现是使用线性同余方法

I understand that what's passed into srand() as an argument will be used as the seed for calls to rand() that happen afterward. Generally time(NULL) is passed in.

But if you only call srand once, and then you have rand being called continuously in a loop, how are the numbers different from each other? The seed was the value returned from time(NULL)...but ONLY that one specific return value is being used. So how are different numbers being generated?

I don't know anything about threading in C. But is srand using threads and a different seed each time so that subsequent calls to rand actually do return random numbers? I noticed that when I pass a static number to srand instead of time(NULL), the rand is still generating random numbers.

解决方案

The draft C99 standard provides a sample implementation in section 7.20.2.2 The srand function that will probably clear up how it work for you:

EXAMPLE The following functions define a portable implementation of rand and srand.

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

void srand(unsigned int seed)
{
    next = seed;
}

srand set the static variable next which is then set to new value in rand and then subsequent calls to rand will pick up with the previous value of next.

For example if we used srand(1) then the sequence would look like this as we make calls to rand:

next = 1
next = 1                   * 1103515245 + 12345  = 1103527590
next = 1103527590          * 1103515245 + 12345  = 1217759518843121895
next = 1217759518843121895 * 1103515245 + 12345  = 15426401326682203284

As HostileFork points out, the numbers are not truly random but are Pseudorandom, which says:

also known as a deterministic random bit generator (DRBG),[1] is an algorithm for generating a sequence of numbers whose properties approximate the properties of sequences of random numbers. The PRNG-generated sequence is not truly random, because it is completely determined by a relatively small set of initial values, called the PRNG's seed (which may include truly random values). Although sequences that are closer to truly random can be generated using hardware random number generators, pseudorandom number generators are important in practice for their speed in number generation and their reproducibility.

This particular implementation is using a Linear congruential generator.

这篇关于如何兰特()做出的随机数,即使你不随时间(NULL)种子呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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