种子rand()为一个C ++类 [英] Seeding rand() for a C++ class

查看:218
本文介绍了种子rand()为一个C ++类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个C ++类,在构造函数中使用 rand()。我真的很喜欢这个类在几乎所有的方式照顾自己,但我不知道在哪里种子 rand()

I am working on a C++ class that uses a rand() in the constructor. I would really like for this class to take care of itself in pretty much every way, but I'm not sure where to seed rand().

如果我在构造函数中种子 rand(),那么每次构造一个我的对象类型的新实例时,它将被种子化。因此,如果我按顺序创建3个对象,它们都将在同一秒钟创建,因此具有相同的种子 rand(),产生完全相同的数据每个对象的3个实例。

If I seed rand() in the constructor, it will be seeded every time a new instance of my object type is constructed. So if I were to create 3 objects in sequence, they would all be created in the same second and therefore have the same seed for rand(), producing the exact same data for each of the 3 instances of the object.

我想在类代码中种子 rand()而不是在我创建对象之前在我的程序的主要功能中做。我想做一个 static bool seeded; 变量,表示 rand()是否已经播种,但是我不知道如何在创建类时将其初始化为false。

I would like to seed rand() within the class code, rather than having to do it in the main function of my program before I create the object. I thought of doing a static bool seeded; variable that signifies whether or not rand() has been seeded yet, but I'm not really sure how to initialize it to false at the creation of the class.

我的想法像

myConstructor(){
    if(!seeded){
        srand(time(NULL));
        seeded = true;
    }

    myVariable = rand()%maxVal;
}

我认为如果我只能弄清楚如何初始化静态值在程序开始时单次为false。我的理解是,如果该对象的静态值为true,则将该静态值更改为true将会在该对象的所有实例中都存在,因此只会在创建对象类型时第一次执行种子函数。

I think this would work if I could just figure out how to initialize the static value to false a single time at the start of the program. It is my understanding that changing this static value to true would carry across all instances of the object if it were static, and would therefore only execute the seed function the very first time that object type is created.

推荐答案


我认为这将工作,如果我只能找出如何初始化静态值为false一次在开始

I think this would work if I could just figure out how to initialize the static value to false a single time at the start of the program.



// my_class.h
class my_class {
public:
  // ...
private:
  static bool seeded;
};

// my_class.cpp
bool my_class::seeded = false;

确保在实现中定义 seeded 文件。否则每个包含你的头的文件都会得到它自己对静态成员的定义,它也可能导致链接器问题,因为它可以多次定义。

Make sure to define seeded in the implementation file. Otherwise every file which includes your header will get its own definition of the static member and it could also cause linker issues as it can be defined more than once.

注意,如果静态成员是一个const整数类型,你可以在声明点分配它。

On a side note, if the static member were of a const integral type, you could assign it at the point of declaration.

另一种选择是这个,个人我更喜欢它对于此任务:

Another option would be this, and personally I would prefer it for this task:

my_class::my_class()         
{
    static bool seeded = false;
    if(!seeded) {
        srand(time(NULL));
        seeded = true;
    }

    myVariable = rand() % maxVal;
}

这篇关于种子rand()为一个C ++类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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