c++ - 随机数生成缺少相同种子的模式

标签 c++ random

我正在为特定种子生成一个随机数...所以我有一个名为 seed 的属性,我允许用户设置它。我给了他两个选项:resettriggerreset 将重新开始生成随机数。而trigger会生成下一个随机数。伪代码有点像

::setSeed( unsigned &  seed )
{
   m_seed = seed
}

::reset()
{
   m_seed = getcurrentseed()
   srand(m_seed);
}

::trigger()
{
    raValue = minValue + ( rand() % (maxValue-minValue+1) );
}

对于一个特定的种子,如果我生成 5 个随机值 5 次..有时我会发现其中一个集合中缺少一个值。可能是什么原因 ?

例如。

seed(5)
rand()
rand()
rand()
seed(5)
rand()
rand()
rand()

在第二次 seed(5) 调用后,有时我会得到不同的数字序列,或者我错过了前一个序列中的数字

void RandomNodeLogic::process(  SingleInputValueGetters const& singleInputValueGetters
                                   , MultiInputValueGetters const& /*multiInputValueGetters*/
                                   , OutputValueKeepers const& /*outputValueKeepers */
                                   , OutputValueSetters const& outputValueSetters )
{
    // get the seed and generate the random number 
    bool doRandom( false );
    int maxValue=  getMaxValue().getAs<int>();
    int minValue = getMinValue().getAs<int>();
    int newValue=0;

    if(minValue > maxValue)
    {
        setMaxValue(minValue);
        setMinValue(maxValue);
        maxValue=  getMaxValue().getAs<int>();
        minValue = getMinValue().getAs<int>();

    }



    SingleInputValueGetters::const_iterator it = singleInputValueGetters.begin();
    for( ; it != singleInputValueGetters.end(); ++it)
    {
        SlotID id = it->first;
        const SlotValue* value = it->second();
        if(!value)
        {
            continue;
        }

        if ( id == RTT::LogicNetwork::RandomNode::nextValuesSlotID )
        {
            doRandom = value->getAs<bool>();
            newValue = minValue + ( rand() % (maxValue-minValue+1) );  // read the value from the next input slot
            setRandomValue( ::convertToSlotValue( m_genValue.m_attrType, newValue ) );
        }

        else if ( id == RTT::LogicNetwork::RandomNode::resetValuesSlotID )
        {
            if ( value->getAs<bool>() )
            {
                doRandom = value->getAs<bool>();
                setSeed(m_seed);
                newValue = minValue + ( rand() % (maxValue-minValue+1) );
                setRandomValue( ::convertToSlotValue( m_genValue.m_attrType, newValue ) );

            }

        }

    }

    if(!m_genValue.empty() && doRandom)
    {
        outputValueSetters.find( RTT::LogicNetwork::RandomNode::outputValuesSlotID)->second( m_genValue.getAs<int>() ) ;
        RTT_LOG_INFO( QString("Random Number: %1").arg( m_genValue.getAs<int>() ));
    }

    if(!doRandom)
    {
        if(m_genValue.empty())
        {
            srand(1);
            m_genValue = 0;
        }
        getAssociatedNode()->sleep();
    }

}



 void RandomNodeLogic::setSeed( const SlotValue& seed )
    {
        SlotValue oldValue = m_seed;
        m_seed = seed;
        srand(m_seed.getAs<unsigned int>());
        modifiablePropertyValueChanged( RTT::LogicNetwork::RandomNode::seedPropertyID, m_seed, oldValue );


}

最佳答案

到目前为止,您提供的所有信息都告诉我您正在尝试为 C 随机函数编写 C++ 类包装器。即使此包装器是一个可以多次实例化的类,底层 C 函数也只能访问一个 状态。这就是为什么你必须确保没有其他人在使用这个包装类的实例。

这就是为什么用 C++ 类包装 C 的随机函数是一个坏主意的原因。作为随机的 C++ 练习,尝试实现您自己的随机类,it's not as hard as it seems .

关于c++ - 随机数生成缺少相同种子的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41717855/

相关文章:

algorithm - 随机 1 对 1 哈希函数

javascript - JS中从大小为n的数组中随机选取m个整数

Python。有没有办法选择一个随机的def?

C++ 在不重定向输出的情况下运行批处理文件

c++ - C++/编译器/重载中的二维数组

c++ - 如何创建自定义 Murmur Avalanche 混合器?

javascript:从范围内生成 2 个随机但不同的数字

c++ - 什么面向方面的语言是 c++ 程序员的好起点

c++ - 配置工厂构建的对象的正确模式

c++ - 使用 7Zlib 命令行将文件解压到目录中