Reputation: 140
I am curious why the following implementation always return 0 using the visual studio compiler, but it works fine when compiled with gcc and clang
int poissrand(double lambda){
int k=0;
double L=exp(-lambda), p=1;
do {
++k;
p *= rand()/(double)INT_MAX;
} while (p > L);
return --k;
}
Examples here (vc) and here (gcc)
Upvotes: 1
Views: 69
Reputation: 34575
In MSVC the range of rand()
is different. In all versions it is specified by RAND_MAX
in stdlib.h
.
If the value you multiply p
by is supposed to be in the range 0..1
then please try
p *= (double)rand() / RAND_MAX;
Upvotes: 4