c++ - 当我正确播种生成器时,为什么我会得到相同的随机数?

标签 c++ random random-seed

我正在尝试制作一个迷你彩票模拟器。我想生成 8 个随机数,最小值为 1,最大值为 20。我将这些生成的数字存储在 Lottery 对象中。我有一个 LotteryManager 类,我可以在其中决定要生成这些彩票多少次。但我总是得到相同的随机数。我给发电机播种了,所以我不明白它有什么问题。如有任何帮助,我们将不胜感激!

这是我的彩票课:

class Lottery
{
private:
    vector<int> lotteryA;
    int lotteryB;
public:
    Lottery();

    vector<int> getLotteryA() const;
    int getLotteryB() const;

    void generateLotteryA();
    void generateLotteryB();
    void printLottery() const;
};

这是管理它的类:

class LotteryManager
{
private:
    unsigned int quanity = 0;
    map<unsigned int, Lottery> lotteries;
public:
    LotteryManager();
    LotteryManager(unsigned int q);

    unsigned int getQuanity() const;
    void setQuanity(unsigned int value);

    void generateLotteries();
    void printLotteries() const;
};

这是我的主要代码:

LotteryManager* lm = new LotteryManager(100);
    lm->generateLotteries();
    lm->printLotteries();

LotteryManager类中的生成和打印函数:

void LotteryManager::generateLotteries()
{
    for(unsigned int i = 0; i < getQuanity(); ++i)
    {
        Lottery l;
        l.generateLotteryA();
        l.generateLotteryB();
        lotteries.insert(make_pair(i, l));
        l.printLottery();
    }
}

void LotteryManager::printLotteries() const
{
    cout << "Lotteries:" << endl;
    for(auto current : lotteries)
    {
        cout << current.first << ".: ";
        current.second.printLottery();
    }
}

以及Lottery类中的生成函数: (生成的A和B只是不同的字段,因为游戏旨在模仿Putto游戏,其中A字段需要从20中猜8个数字,B字段需要从4中猜1)

void Lottery::generateLotteryA()
{
    random_device rn_d;
    mt19937 rng(rn_d());
    uniform_int_distribution<int> dist(1, 20);
    for(unsigned int i = 0; i < 8; ++i)
    {
        lotteryA.push_back(dist(rng));
    }
}

void Lottery::generateLotteryB()
{
    srand(time(0));
    lotteryB = (rand() % 20) + 1;
}

提前致谢!

最佳答案

Lottery::generateLotteryA 中,您声明一个随机引擎和随机设备。每次调用此函数时,引擎都会使用相同的随机设备进行播种,因此您可以获得相同的结果。


每次使用不同的 random_device 调用 rn_d() 会生成不同的值,这似乎是合理的。然而,这不是必需的,正如 reference 中所述。 :

... each std::random_device object may generate the same number sequence.

实际上,大多数实现生成不同的值,因此这并不是真正的问题。您甚至可以将其视为标准库中没有做出此保证的缺陷。但是,根据当前的指定,您可以看到生成的相同值。


最简单的修复方法是使引擎静态,因此它仅在第一次调用函数时获得种子:

void Lottery::generateLotteryA()
{
    // ...
    static mt19937 rng(rn_d());
    // ...
}

关于c++ - 当我正确播种生成器时,为什么我会得到相同的随机数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64876085/

相关文章:

c++ - MSVC++属性选项卡上的项目是什么意思?

c++ - 要使用 boost::object_pool 为 map 实现自定义分配器,How to allocate n continuous elements using boost::object_pool?

C++ GMP 生成随机数

c++ 为 psudo 随机数生成器生成一个好的随机种子

java - PRNG 在 java 中按键播种

r - 使用 cv.glmnet 并行设置种子在 R 中给出不同的结果

c# - 如何 : Monitoring file access by another process in Windows?

c++ - 为什么我需要同时包含 iostream 和 fstream header 才能打开文件

c++ - 在一系列值之间生成随机 double

r - 从数据帧进行分层随机抽样