Generation random numbers C ++

0

Hello, I am writing a C ++ simulation and I have to generate some random numbers. For this, I initialize (srand (time (NULL)) once only and I have a method as follows:

void Supermercado::geraTempoProximoCliente() {
    int intervaloProximoCliente = rand()%(2*tempoMedioChegada-1) +1;

    printf("Intervalo para chegar outro cliente %d\n",intervaloProximoCliente);

    tempoChegadaProximoCliente = getRelogio() + intervaloProximoCliente;
}

Can anyone tell me why every time my program generates a new interval, is it always the same throughout the program? Constructor where seed is generated:

Supermercado::Supermercado(string nome_,int tempoSimulacao_,inttempoMedioChegada_,
    int numeroDeCaixas_, int tamanhoMaximoDasFilas, Caixa caixas[]) {
listaDeCaixas = new CircularList<Caixa>();
tempoMedioChegada = tempoMedioChegada_;
relogio = 0;
clientesDesistentes = 0;
valorDesistentes = 0;
numeroDeCaixas = numeroDeCaixas_;
tempoSimulacao = tempoSimulacao_;
tempoChegadaProximoCliente = 0;
srand(time(NULL));
for(int i = 0; i < numeroDeCaixas; i++)
    listaDeCaixas -> push_back(caixas[i]);
}

I instantiate the supermarket once just in the main and call the execute method of it.

The execute method:

void Supermercado::executa() {
    bool continua = true;
    Caixa caixa;
    geraTempoProximoCliente();
    while(relogio != tempoSimulacao*3600) {
        if(relogio == tempoChegadaProximoCliente)
            geraTempoProximoCliente();
    }
    relogio++;
}
    
asked by anonymous 18.10.2016 / 14:21

2 answers

1

C ++ 11 can already be considered standard, and you have introduced a default library for random numbers, <random> , even if it is not the source of your problem, I'll introduce you to it.

The main problem of using rand() and truncating bits with % is that the distribution of bits in rand is not guaranteed to be uniform. Even though it works for you, I recommend using the new libraries that guarantee the default implementation of the pseudorandom generator.

Basically, you need to declare a number generator. One of those provided by the standard library is a 32-bit Mersenne Twister:

#include <random>
std::mt19937 mt(42); //gerador mersene twist, inicializado com seed = 42

Then you need to define how you want to map its pseudorandom output (rather than improvise with the % operator). If you want integers between zero and one hundred, evenly distributed, for example:

//distribuição linear de inteiros entre 0 e 100
std::uniform_int_distribution<int> linear_i(0, 100);

Whether you want real numbers linearly distributed between 0.0 and 100.0, for example:

//distribuição linear de números reais entre 0 e 100
std::uniform_real_distribution<float> linear_r(0.f, 100.f);

After initializing the generator and distribution (you can save them as a member of the class), every time you need a number, simply call the desired distribution to map a number generated by the generator:

int   aleatorio_i = linear_i( mt ); //inteiro pseudo-aleatório
float aleatorio_f = linear_r( mt ); //float pseudo-aleatório

You can test the example I created here .

The best thing about the new library is that it has several ready-made distributions. The case you present - the interval between customer arrivals - for example, can be modeled by a normal distribution around the mean.

A while back I made an example code of the distributions . Maybe it will be a guide.

    
20.10.2016 / 23:02
-1

Always put srand () function before the rand () function. How do you call rand () in

void Supermercado::geraTempoProximoCliente()

Then put srand () before the code

int intervaloProximoCliente = rand()%(2*tempoMedioChegada-1) +1;

Ending like this:

void Supermercado::geraTempoProximoCliente() {
    srand(time(NULL));
    int intervaloProximoCliente = rand()%(2*tempoMedioChegada-1) +1;
    printf("Intervalo para chegar outro cliente %d\n",intervaloProximoCliente);
    tempoChegadaProximoCliente = getRelogio() + intervaloProximoCliente;
}

By doing this, you ensure that every time you generate a new number, the program captures a new system time to use in the rand () function.

I hope I have helped!

    
19.10.2016 / 19:30