函数srand(时间(NULL))不会改变种子值够快 [英] srand(time(NULL)) doesn't change seed value quick enough

查看:652
本文介绍了函数srand(时间(NULL))不会改变种子值够快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用C编写的一个简单的随机数发生器 int类型l 是下界和 INT U 是上限

它工作得很好,但我有一个关于播种它的问题。如果我是在一个循环中,时间(NULL)运行此不改变种子值速度不够快,能够为prevent得到一系列连续的随机是完全相同的编号。

我不知道如何任何人可能接触过这个问题。所有的例子,我发现在网上使用时间(NULL)作为种子值生成。

  INT generateRandom(INT L,诠释U)
{
   函数srand(时间(NULL));   INT R =兰特()%((U - 1)+ 1);
   R = L + R;   返回ř;
}

如果我是紧挨着对方运行code的这些线,既简贾尼丝 RAND2 应该是完全一样的。

 的printf(=简贾尼丝%d个\\ N,generateRandom(10,46));
的printf(RAND2 =%d个\\ N,generateRandom(10,46));


解决方案

函数srand(时间(NULL))应运行一次以intialise的PRNG。在主做这个应用程序启动时。

说明:

一个PRNG(伪随机数生成器)产生依赖于所使用的算法号码确定性序列。一个给定的算法总是会从给定的起始点(种子)产生相同的序列。如果不明确地播种PRNG那么它通常会从相同的默认种子每一个应用程序运行时启动,导致相同的数字序列被使用。

要解决这个问题,你需要播种PRNG自己与不同的种子(给不同的序列),每个应用程序运行时间。通常的做法是用时间(NULL)这将根据当前时间的种子。只要你不对方一秒钟内启动应用程序的两个实例,你会得到保证不同的随机序列。

有没有需要你想有一个新的随机数,每次种子序列。而且我不知道这一点,但我有取决于PRNG算法上正在使用的重新播种为每个新号的感觉实际上可能会导致产生的顺序降低随机性。

I have written a simple random number generator in C. int l is the lower bound and int u is the upper bound.

It works just fine, however I have a question regarding seeding it. If I was to run this in a loop, time(NULL) doesn't change the seed value quick enough to be able to prevent getting a consecutive series of random numbers that are exactly the same.

I'm wondering how anybody else might have approached this problem. All the examples I've found online use time(NULL) as the seed value generator.

int generateRandom(int l, int u)
{
   srand(time(NULL));

   int r = rand() % ((u - l) + 1);
   r = l + r;

   return r;
}

If I was to run these lines of code right next to each other, both Rand1 and Rand2 would be exactly the same.

printf("Rand1 = %d\n", generateRandom(10, 46));
printf("Rand2 = %d\n", generateRandom(10, 46));

解决方案

srand(time(NULL)) should be run exactly once to intialise the PRNG. Do this in Main when the application starts.

Explanation:

A PRNG (Pseudo-Random Number Generator) generates a deterministic sequence of numbers dependent on the algorithm used. A given algorithm will always produce the same sequence from a given starting point (seed). If you don't explicitly seed the PRNG then it will usually start from the same default seed every time an application is run, resulting in the same sequence of numbers being used.

To fix this you need to seed the PRNG yourself with a different seed (to give a different sequence) each time the application is run. The usual approach is to use time(NULL) which sets the seed based on the current time. As long as you don't start two instances of the application within a second of each other, you'll be guaranteed a different random sequence.

There's no need to seed the sequence each time you want a new random number. And I'm not sure about this, but I have the feeling that depending on the PRNG algorithm being used re-seeding for every new number may actually result in lower randomness in the resulting sequence.

这篇关于函数srand(时间(NULL))不会改变种子值够快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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