S. Proa - 1 year ago 119
C Question

# Random numbers with C using a numerical recipe

I wanted to get a matrix with random numbers following a normal distribution and I wrote this code:

``````#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define T 1
#define dt 0.2
#define iter (T/dt)

#define TWOPI (6.2831853071795864769252867665590057683943387987502)

/*See page 306 of 949 Numerical Recipes.*/
#define IM1 2147483563
#define IM2 2147483399
#define AM (1.0/IM1)
#define IMM1 (IM1-1)
#define IA1 40014
#define IA2 40692
#define IQ1 53668
#define IQ2 52774
#define IR1 12211
#define IR2 3791
#define NTAB 32
#define NDIV (1+IMM1/NTAB)
#define EPS 1.2e-7
#define RNMX (1.0-EPS)

float ran2(int *idum){
int j;
long k;
static long idum2=123456789;
static long iy=0;
static long iv[NTAB];
float temp;

if (*idum <= 0) {
if (-(*idum) < 1) *idum=1;
else *idum = -(*idum);
idum2=(*idum);
for (j=NTAB+7;j>=0;j--) {
k=(*idum)/IQ1;
*idum=IA1*(*idum-k*IQ1)-k*IR1;
if (*idum < 0) *idum += IM1;
if (j < NTAB) iv[j] = *idum;
}
iy=iv[0];
}
k=(*idum)/IQ1;
*idum=IA1*(*idum-k*IQ1)-k*IR1;
if (*idum < 0) *idum += IM1;
k=idum2/IQ2;
idum2=IA2*(idum2-k*IQ2)-k*IR2;
if (idum2 < 0) idum2 += IM2;
j=iy/NDIV;
iy=iv[j]-idum2;
iv[j] = *idum;
if (iy < 1) iy += IMM1;
if ((temp=AM*iy) > RNMX) return RNMX;
else return temp;
}
/*End of the recipe.*/

double RANDN(int seed){
return sqrt(-2.0*log(ran2(&seed)))*cos(TWOPI*ran2(&seed));
}

double rnd(){
return (double) rand() / (double) RAND_MAX;
}

int main(){
int i,j,k;
double **x;
x=(double **)malloc(2*sizeof(double*));

for(k=0; k<2; k++){
x[k]=(double*)malloc(iter*sizeof(double));
}

srand(time(NULL));
for(i=0; i<2; i++){
for(j=0; j<10; j++){
x[i][j]=RANDN(rnd()+2);
printf("%lf\n",x[i][j]);
}
}
free(x);
return 0;
}
``````

In order to do obtain satisfactory random numbers I used a recipe of the book Numerical Recipes, and to get the random numbers following a normal distribution I wrote the function RANDN(). The seed of this function should be different each time, so I put as a seed just the tipical random function of C.

The problem is that each time I execute the code I get the same numbers. I do not get a new list of random numbers. What may be the problem?

The function `RANDN` takes an `int`, and yet you pass it a `double`, using the function `rnd`. The `double` returned from that function is in the range [0.0,1.0]. Then 2 is added to that value and the result is truncated to an `int`, always1 passing the value 2 to `RANDN`.
1 Since the formula for getting the range is `(double) rand() / (double) RAND_MAX;`, the function `rnd` could return the value 1.0, which would result in passing 3, not 2, to `RANDN`. This will happen very rarely.