random numbers, min and max

Hey everyone, I want to add a range for my random distribution but I did'nt succeed, need some help. I want to put a range max value and min value for the random distribution.

here is my distribution :



[ real d, va_unif1,va_unif2,va_gauss;
real pi;
real c;
real mu;
real sig;
real dmin;
real dmax;
/*real d=P_DIAM(p);*/

pi = 2.*acos(0.);
c = 1811;
sig = 0.37;
mu = -0.67;

va_unif1 = (real) rand() / (real) RAND_MAX;
va_unif2 = (real) rand() / (real) RAND_MAX;
va_gauss = sqrt(-2.*log(va_unif1))*cos(2*pi*va_unif2);
P_DIAM(p)=exp(mu+sig*va_gauss);]


Thanks for your help.
Cheers,

Souria
Assuming that real is a typedef for double, there are only two lines here which seem related to your question:
1
2
        real va_unif1 = (real) rand() / (real) RAND_MAX;
        real va_unif2 = (real) rand() / (real) RAND_MAX;

this generates a pair of floating point numbers in the range between 0 and 1.

The question then is what range do you want it to be, and what did you try?
1
2
3
4
const int MIN 50
const int MAX 100

double randNumber = MIN + (rand() % (MAX - MIN));
Last edited on
1
2
3
va_unif1 = (real) rand() / (real) RAND_MAX;
va_unif2 = (real) rand() / (real) RAND_MAX; 
va_gauss = sqrt(-2.*log(va_unif1))*cos(2*pi*va_unif2);

What happens when va_unif1 is zero?
I want to put a range : min = 0.375 and a max = 250
Then take your existing va_unif1 and scale it to the correct range which is (max - min), then add min.
I use this but I get a weired numbers ( an eg. : -1.#IND0e+00) !

real min;
real max;
va_unif1 = (max-min)*(real) rand() / ((real) RAND_MAX + min);
va_unif2 = (max-min)*(real) rand() / ((real) RAND_MAX + min);
But your values 0.375 and 250 don't appear anywhere in your code?
I didn't give you the whole one :
real d, va_unif1,va_unif2,va_gauss;
real pi;
real c;
real mu;
real sig;
real max;
real min;

/*real d=P_DIAM(p);*/

pi = 2.*acos(0.);
sig = 0.37;
mu = -0.67;
max = 250;
min = 0.375;
va_unif1 = (max-min)*(real) rand() / ((real) RAND_MAX + min);
va_unif2 = (max-min)*(real) rand() / ((real) RAND_MAX + min);
va_gauss = sqrt(-2.*log(va_unif1))*cos(2*pi*va_unif2);
P_DIAM(p)=exp(mu+sig*va_gauss);
This expression doesn't do what I suggested:
va_unif1 = (max-min)*(real) rand() / ((real) RAND_MAX + min);
your code is now dividing by ((real) RAND_MAX + min)



take the original code:
va_unif1 = (real) rand() / (real) RAND_MAX;

multiply the right-hand side by the factor (max-min):
va_unif1 = (max-min)*((real) rand() / (real) RAND_MAX);

then add min
va_unif1 = (max-min)*((real) rand() / (real) RAND_MAX) + min;


Topic archived. No new replies allowed.