float gasdev(void) { /* float ran3(long *idum); */ static int iset=0; static float gset; float fac, rsq, v1, v2, temp; /* We don't have an extra deviate handy, so pick 2 uniform numbers in the square extending from -1 to +1 in each direction, see if they are in the unit circle, and if they are not, try again.*/ if (iset == 0){ do{ v1 = 2.0*rand0to1()-1.0; v2 = 2.0*rand0to1()-1.0; rsq=v1*v1+v2*v2; }while (rsq >= 1.0 ||rsq == 0.0); temp = -2.0*log(rsq)/rsq; if (temp >= 0.0) fac = sqrt(temp); else { printf("error in gasdev\n"); } /* Now make the Box-Muller transformation to get 2 normal deviates. Return one and save the other for the next time. */ gset = v1*fac; iset=1; /*set flag */ return v2*fac; } else{ /* We have an extra deviate handy, so unset the flag, and return it.*/ iset = 0; return gset; } }
/* Create an array of random integers between the range [min,max) */ int * randintarray(const int size,const int Min,const int max){ int i, * array; array = (int *) malloc(size*sizeof(int)); for(i=0;i<size;i++) array[i] = Min + floor((double)max*rand0to1()); return array; }
int rand_in_rad(const float rad, float *x, float *y){ int found=0; float tx,ty; do{ tx = 2 * rad * (rand0to1()-0.5); ty = 2 * rad * (rand0to1()-0.5); if( rad > sqrt( pow(tx,2) + pow(ty,2) ) ){ found = 1; } }while(!found); *x = tx; *y = ty; return 0; }
/* Create an array of random integers between the range [min,max) */ int * randboolarray(const int size){ int i, * array; float v; array = (int *) malloc(size*sizeof(int)); for(i=0;i<size;i++){ v=rand0to1(); if(v<0.5) array[i] = 0; else array[i] = 1; } return array; }