#include "../kmat.h" VALUE kmgv_mat_random; // return a random number which follows N(0, 1) // `random' is an instance of Random double km_rand_normal(VALUE random) { if (RTEST(rb_ivar_get(random, id_iv_kmat_stored))) { rb_ivar_set(random, id_iv_kmat_stored, Qfalse); return NUM2DBL(rb_ivar_get(random, id_iv_kmat_stored_value)); } else { rb_ivar_set(random, id_iv_kmat_stored, Qtrue); double sqrt_m2_log_x = sqrt(-2.0*log1p(-NUM2DBL(rb_funcall(random, id_rand, 0)))); double two_pi_y = M_2PI*NUM2DBL(rb_funcall(random, id_rand, 0)); rb_ivar_set(random, id_iv_kmat_stored_value, rb_float_new(sqrt_m2_log_x*sin(two_pi_y))); return sqrt_m2_log_x*cos(two_pi_y); } } // replace ary[0..(len-1)] by random numbers which follow N(0, 1) // `random' is an instance of Random void km_fill_normal(int len, double *ary, VALUE random) { int i; double sqrt_m2_log_x, two_pi_y; len--; if (RTEST(rb_ivar_get(random, id_iv_kmat_stored))) { ary[0] = NUM2DBL(rb_ivar_get(random, id_iv_kmat_stored_value)); i = 1; } else { i = 0; } for ( ; i