我正在尝试通过泊松分布为我的项目生成随机到达。代码部分:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <random>
#include <ctime>
int nextTime(double lambda,int timeslots,int PU_number);
int main()
{
srand(time(0));
//some code
nextTime(4,time_slots,PU_number);
}
int nextTime(double lambda,int timeslots,int PU_number)
{
int mat[PU_number][timeslots];
std::default_random_engine generator;
std::poisson_distribution<int> distribution(lambda);
int number;
int total=0;
for(int i=0; i< PU_number;i++)
{
total=0;
for(int j=0;j<timeslots;j++)
{
number = distribution(generator);
total+=number;
mat[i][j]=total;//another matrix, not relevant here
cout<<number<<" ";
}
cout<<endl;
}
return 0;
}
以上代码始终生成相同的数字。这有什么不对?
答案 0 :(得分:2)
srand
种子rand
,而不是您的std::default_random_engine
。要播种,请使用
std::default_random_engine generator(std::random_device{}());
^^^^^^^^^^^^^^^^^^^^^^
e.g., any seed can go here