boxmuler.c revision df3eb16e38c6a163b0a7367c885679eed6140964
1#include <math.h> 2#include <stdlib.h> 3 4#define M_2PI (M_PI*2) 5 6int box_muler(int min, int max) 7{ 8 double u1,u2,z; 9 int i; 10 int ave; 11 int range; 12 int ZZ; 13 if (min >= max ) { 14 return (-1); 15 } 16 range = max - min; 17 ave = range/2; 18 for (i = 0 ; i< 10 ; i++ ) { 19 u1 = ((double)(random() % 1000000))/ 1000000; 20 u2 = ((double)(random() % 1000000))/ 1000000; 21 z = sqrt( -2.0 * log(u1) ) * cos ( M_2PI * u2 ); 22 ZZ = min + (ave + (z*(ave/4))); 23 if (ZZ >= min && ZZ < max ) { 24 return (ZZ); 25 } 26 } 27 return (-1); 28} 29