vendredi 10 mai 2019

How can I make sure my random number between 0 and 1 generated by rand is not 0?

I'm working on simulating a queuing model and I need to generate my arrivals using this algorithm. However I sometimes get 0.000000 as a random value R1 and it is not possible to calculate ln(0) therefore the process stops. How can I avoid getting a 0?

I thought about adding 0.0000001 to each, but wouldn't that be bad too because I might get something greater than 1?

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>

main()
    {
        int a=3, b=4, T=500, I=0, l=1504;
        srand(time(NULL));
        float R1, R2, t=0, S[10000], f;

        printf("Utilizaremos la funcion at+b para el simular el poceso\n");
        while(t<T){
            R1=rand()/(double)RAND_MAX;
            t= t-log(R1)/l;
            R2=rand()/(double)RAND_MAX;
            f = a*t+b;
            if(R2<f/l){
                I=I+1;
                S[I]=t;
                printf(" \n I %d, R1 %f, R2 %f, t %f, S[i] %f", I,R1,R2,t,S[I]);
            }


        }
        getche();
        return 0;
}




Aucun commentaire:

Enregistrer un commentaire