C ++快速随机数发生器 [英] C++ fast random number generator

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

问题描述

对不起,如果这是一个愚蠢的问题,但我是新来的c ++,老实找不到答案;

I'm sorry if this is kind of a dumb question, but I'm new to c++, and honestly can't find the answer;

当我使用 rand(),当然我必须首先使用srand()。

When I use rand(), of course I have to first use srand().

刚开始,我只需导入< ctime> ,然后执行 srand ()),这工作。但是,如果我每秒钟多次调用 rand() - time()得到相同的答案。例如:

At first i'd just import <ctime> and do srand(time()), and this worked. But if I called rand() more than once a second - how often time() changes - then I'd get the same answer. So for instance;

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
    bool x = true;
    while(x == true)
    {
        int num = 1;
        srand(time(NULL));
        num = rand();
        cout<<num%10<<endl;
    }

}

可能会产生类似的东西,6666666666777777777700000000003333333333

Might produce something like, 6666666666777777777700000000003333333333

这不利于我的目的 - 我更喜欢像163509284749301935766。

Which is no good for my purposes - I'd prefer something like 163509284749301935766.

推荐答案

您应该只对随机数生成器种一次。现在你正在播种它的循环,并使用 time(NULL)只是意味着种子每秒更改一次,这给你的错误输出你描述。

You should only seed the random number generator once. Right now you are seeding it in the loop and using time(NULL) just means the seed changes once per second which gives you the bad output you have described.

请改为:

int main()
{
    bool x = true;
    int num = 1;
    srand(time(NULL));
    while(x == true)
    {
        num = rand();
        cout<<num%10<<endl;
    }
}

如果你真的关心生成的随机数可能想使用除 rand()之外的其他东西。原因是 rand()对于伪随机数生成具有差的统计属性,它通常被实现为线性同余发生器。如果你需要高质量的随机性,那么你应该喜欢别的东西,如新的c ++随机数生成器 http ://en.cppreference.com/w/cpp/numeric/random
实际上,甚至有一个报告贬值旧的 rand(),试图推动人们使用新的c ++标准库随机函数。

And if you really care about the random numbers generated you might want to use something other than rand(). The reason is that rand() has poor statistical properties for pseudo random number generation, it is often implemented as a Linear congruential generator. If you need high quality randomness then you should prefer something else such as the new c++ random number generators http://en.cppreference.com/w/cpp/numeric/random. In fact there's even a report on depreciating the old rand() to try to push people to use the newer c++ standard library random functions.

在这种特殊情况下,你需要一个模数,这会导致一些细微的问题:

In this particular case you take a modulus which causes a few subtle problems:

num = rand();
cout<<num%10<<endl;

即使 rand()这里的模数不是 rand()返回的最大值的除数,因此会得到一个非均匀分布。这里有一个快速的解释,说 rand()返回的值在[0,25]范围内,然后取模量将执行以下操作。

Even if rand() was perfect if the modulus here isn't a divisor of the maximum value returned by rand() you will get a non-uniform distribution as a result. Here's a quick explanation, say rand() returned values in the range of [0,25] then taking the modulus would do the following.

before    after modulus 
[0-9]         [0-9]
[10-19]       [0-9]
[20-25]       [0-5]

你会看到得到[0-5]比[6-9],这意味着你现在不再有一个统一的数字被生成。注意,这个小范围仅用于教育目的, rand()的最大值被标准强制为至少32767.然而,它说明了一个重要的点,最大生成数越大越好。

You'll see that you are more likely to get [0-5] than [6-9] which means you now no longer have a uniform number being generated. Note that this small range is for educational purposes only, the maximum value of rand() is mandated by the standard to be at least 32767. However it illustrates an important point, the larger the maximum generated number the better.

除了模数之外的分布问题的均匀性对于某些实现甚至进一步具有降低伪随机性的质量的特别隐蔽的效果。

This uniformity of distribution problem aside the modulus has the particularly insidious effect of decreasing the quality of the pseudo-randomness even further for some implementations.

使用 std :: uniform_int_distribution 避免了许多问题,因此我建议更改现有代码以使用新库。这样做看起来像这样:

Using std::uniform_int_distribution avoids many problems so I would recommend changing your existing code to use the new library. Doing so would look like this:

#include <iostream>
#include <random>
using namespace std;

int main()
{
    std::default_random_engine generator;
    generator.seed( /* your seed for the RNG goes here */ );
    std::uniform_int_distribution<int> distribution(0,9);//note the min and max parameters are inclusive here
    while(true)
    {
        cout << distribution(generator) << endl;
    }

}

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

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