C++ 如何在C++;不把时间当作种子
我的程序应该生成最大固定值为9999的随机数。这些数字用于创建对象。现在的问题是,当使用时间作为种子时,如果我一个接一个地创建两个对象,这两个对象都将使用相同的编号创建(因为在此过程中时间没有改变)C++ 如何在C++;不把时间当作种子,c++,c++11,random,C++,C++11,Random,我的程序应该生成最大固定值为9999的随机数。这些数字用于创建对象。现在的问题是,当使用时间作为种子时,如果我一个接一个地创建两个对象,这两个对象都将使用相同的编号创建(因为在此过程中时间没有改变) 如何更有效地执行此操作,以便每次运行程序时都能生成一个不同的随机数?关键是-仅创建一次随机对象(或其状态),然后重用它以获得下一个值。这种方法通常在其他语言中使用,例如Java、C#、Python 对于较旧的C风格示例,请参见,这是您感兴趣的部分 //initialize random seed O
如何更有效地执行此操作,以便每次运行程序时都能生成一个不同的随机数?关键是-仅创建一次随机对象(或其状态),然后重用它以获得下一个值。这种方法通常在其他语言中使用,例如Java、C#、Python 对于较旧的C风格示例,请参见,这是您感兴趣的部分
//initialize random seed ONCE
srand (time(NULL));
// call MANY TIMES for next value
rand();
rand();
rand();
同样的想法,例如
//安装一次
std::随机_装置rd;
标准:mt19937 gen(rd());
标准:统一内部分布图(19999);
//多次呼叫以获取下一个随机数
dis(gen);
dis(gen);
dis(gen);
为了更好地理解,我向您展示了线性同余生成器的工作原理
对于这种类型的发电机,数学非常简单:
xi+1=(A*xi+B) mod N
其中A、B和N是常数
代码可以是:
static unsigned int x; //the seed (time in your case)
unsigned int myRand(){
x=A*x+B; //We do not need modulo because it is implicit. For 32b integer the modulo will be 2^32.
return x;
}
#include <iostream>
#include <time.h>
#include <stdlib.h>
int main() {
srand(time(NULL));
for (unsigned int i=0; i<10;i++){
std::cout << random()%(9999+1)<<std::endl;
}
}
因此,当您设置x=time时,当时间不变时,您只需重置生成器即可
因此,您的问题的解决方案可以是:
static unsigned int x; //the seed (time in your case)
unsigned int myRand(){
x=A*x+B; //We do not need modulo because it is implicit. For 32b integer the modulo will be 2^32.
return x;
}
#include <iostream>
#include <time.h>
#include <stdlib.h>
int main() {
srand(time(NULL));
for (unsigned int i=0; i<10;i++){
std::cout << random()%(9999+1)<<std::endl;
}
}
#包括
#包括
#包括
int main(){
srand(时间(空));
对于(未签名int i=0;i一个简单的C++类生成随机数。头文件.< /p>
#include <stdio.h>
#include <tchar.h>
#include <random>
#include <iostream>
#include <algorithm>
#include <vector>
namespace Random
{
/// <summary>
/// Secure pseudo random number generator.
/// </summary>
class RandomGenerator
{
public:
/// <summary>
/// Random number generator.
/// </summary>
RandomGenerator();
/// <summary>
/// Random number generator destructor.
/// </summary>
~RandomGenerator();
/// <summary>
/// Get the randomly ordered numbers between the lower bound and upper bound inclusive. Each random value only occurs once.
/// </summary>
/// <param name="lowerBound">The smallest random value to generate.</param>
/// <param name="upperBound">The largest random value to generate.</param>
/// <returns>The list of randomly ordered values.</returns>
/// <exception cref="std::invalid_argument">Invalid parameters.</exception>
std::vector<int> GetRandomList(int lowerBound = 1, int upperBound = 52);
/// <summary>
/// Get the random number between the lower bound and upper bound inclusive.
/// </summary>
/// <param name="lowerBound">The smallest random value to generate.</param>
/// <param name="upperBound">The largest random value to generate.</param>
/// <returns>A random value.</returns>
/// <exception cref="std::invalid_argument">Invalid parameters.</exception>
int Generate(int lowerBound, int upperBound);
private:
bool _disposed;
};
}
#包括
#包括
#包括
#包括
#包括
#包括
名称空间随机
{
///
///安全伪随机数发生器。
///
类随机发生器
{
公众:
///
///随机数发生器。
///
随机发生器();
///
///随机数发生器析构函数。
///
~RandomGenerator();
///
///获取下限和上限(含上限)之间的随机顺序数字。每个随机值只出现一次。
///
///要生成的最小随机值。
///要生成的最大随机值。
///随机排列的值的列表。
///无效参数。
std::vector GetRandomList(int lowerBound=1,int upperBound=52);
///
///获取下限和上限(含上限)之间的随机数。
///
///要生成的最小随机值。
///要生成的最大随机值。
///一个随机值。
///无效参数。
int-Generate(int-lowerBound,int-upperBound);
私人:
布卢;
};
}
和代码文件
#include "RandomGenerator.h"
using namespace Random;
/// <summary>
/// Random number generator.
/// </summary>
RandomGenerator::RandomGenerator() : _disposed(false)
{
}
/// <summary>
/// Random number generator destructor.
/// </summary>
RandomGenerator::~RandomGenerator()
{
// If not disposed.
if (!_disposed)
{
// Indicate that dispose has been called.
_disposed = true;
}
}
/// <summary>
/// Get the randomly ordered numbers between the lower bound and upper bound inclusive. Each random value only occurs once.
/// </summary>
/// <param name="lowerBound">The smallest random value to generate.</param>
/// <param name="upperBound">The largest random value to generate.</param>
/// <returns>The list of randomly ordered values.</returns>
/// <exception cref="std::invalid_argument">Invalid parameters.</exception>
std::vector<int> RandomGenerator::GetRandomList(int lowerBound, int upperBound)
{
// Validate input.
if (lowerBound > upperBound)
throw std::invalid_argument("The parameters are invalid, the lower bound can not be larger then the upper bound.");
std::vector<int> numbers;
int arraySize = upperBound - lowerBound + 1;
// Allocate the total number of values.
numbers.resize(arraySize);
// For each value in the bounds.
for (int i = lowerBound; i <= upperBound; i++)
{
// Assign the numbers.
numbers[i - lowerBound] = i;
}
// Non-deterministic generator
// to seed mersenne twister.
// Random values on each execution.
std::random_device rd;
std::mt19937 engine(rd());
// Shuffle the numbers between the lower and upper bounds.
std::shuffle(numbers.begin(), numbers.end(), engine);
// Return the randomly ordered numbers.
return numbers;
}
/// <summary>
/// Get the random number between the lower bound and upper bound inclusive.
/// </summary>
/// <param name="lowerBound">The smallest random value to generate.</param>
/// <param name="upperBound">The largest random value to generate.</param>
/// <returns>A random value.</returns>
/// <exception cref="std::invalid_argument">Invalid parameters.</exception>
int RandomGenerator::Generate(int lowerBound, int upperBound)
{
// Validate input.
if (lowerBound > upperBound)
throw std::invalid_argument("The parameters are invalid, the lower bound can not be larger then the upper bound.");
int randomValue;
// Non-deterministic generator
// to seed mersenne twister.
// Random values on each execution.
std::random_device rd;
std::mt19937 engine(rd());
// Use the uniform distribution to generate
// a value between lower and upper bound inclusive.
std::uniform_int_distribution<> dist(lowerBound, upperBound);
// Generate the random value.
randomValue = dist(engine);
// Return the random value.
return randomValue;
}
#包括“RandomGenerator.h”
使用名称空间随机;
///
///随机数发生器。
///
RandomGenerator::RandomGenerator()
{
}
///
///随机数发生器析构函数。
///
RandomGenerator::~RandomGenerator()
{
//如果没有处理。
如果(!\u已处置)
{
//指示已调用dispose。
_这是真的;
}
}
///
///获取下限和上限(含上限)之间的随机顺序数字。每个随机值只出现一次。
///
///要生成的最小随机值。
///要生成的最大随机值。
///随机排列的值的列表。
///无效参数。
std::vector RandomGenerator::GetRandomList(整数下限,整数上限)
{
//验证输入。
如果(下限>上限)
throw std::invalid_参数(“参数无效,下限不能大于上限”);
std::向量数;
int arraySize=上限-下限+1;
//分配值的总数。
数字。调整大小(数组化);
//对于边界中的每个值。
for(int i=下界;i上界)
throw std::invalid_参数(“参数无效,下限不能大于上限”);
int随机值;
//非确定性发生器
//种下梅森麻花。
//每次执行时的随机值。
std::随机_装置rd;
标准:mt19937发动机(rd());
//使用均匀分布生成
//介于下限和上限(含上限)之间的值。
标准:均匀分布区(下界,上界);
//生成随机值。
随机值=距离(发动机);
//返回随机值。
返回随机值;
}
你尝试过文档吗?具体来说,STD::RealthyDead可以用来植入伪随机算法。请给出相关的代码:同时考虑:使用<代码> STD::TROOO。还有,“使用时间”是什么意思?你在每个代码< <代码>种子< /代码> > RAND 调用(如果你使用<代码> RAND )?如果我一个接一个地创建两个对象,这两个对象都将使用相同的数字创建。不要每次需要随机数时都重新设置随机数生成器的种子。请在main()的开始处种子设定一次
如果您需要多个生成器,可能是出于多线程的原因,use可以使用主生成器为每个新的生成器设置种子。@drescherjm:据我所知,该程序只输出一个随机数,需要重新启动以获取其他值,因为问题带有C++11标记,所以发布解决方案可能更合适n这不涉及旧的rand
功能。问题标题还专门询问如何在不使用时间作为种子的情况下生成随机数。@JasonR如果你阅读了问题,你就会意识到“不使用时间”这是一个错误的问题,一个XY问题。真正的问题是生成了相同的数字。OP错误地得出结论,原因一定是时间被用作种子。我不确定哪个范围更适合Othmane Qadiri。他只说最大值是9999。那么,这就够公平了。