WhoCares
WhoCares

Reputation: 225

poisson distribution always produces same instances at each run

I am trying to generate random arrivals via poisson distribution for my project. The code part:

#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;
}

Above code always produces same numbers. what is wrong here?

Upvotes: 1

Views: 609

Answers (1)

Baum mit Augen
Baum mit Augen

Reputation: 50061

srand seeds rand, not your std::default_random_engine. To seed this, use

std::default_random_engine generator(std::random_device{}());
                                     ^^^^^^^^^^^^^^^^^^^^^^
                                     e.g., any seed can go here

Upvotes: 2

Related Questions