Ok... again many thanks for all the help i have previously received, am now struggling with a new problem...
I want to use one of the numbers generated from the ran2 program to initialize a new program to give me a random number that lies within a Gaussian distribution between +1 and -1.
Ive had a bit of a bash at it, trying to follow what you guys had done to help me out with the other number set.
Its actually cool that the ran2 function gives me 9numbers at a time as im working in 8dimensions... (haha, dont ask- we dont know either- just geometry) and to get 9 gaussians from 9 inputs would be awsome... (cant believe i just wrote that)
I know i can do these things using very simple functions- but the "randomness" of what i get out is very influencial when testing the theory, so apologies for my over-complication of the number generation (so he says!)
Here is the function i am trying to use, as on disc, except using my ran2 answers instead of the ran1 as stated in the book:
Code:
/*Normal (Gaussian) deviate p289*/
#include <math.h>
float gasdev(long *idum)
{
float ran1(long *idum);
static int iset=0;
static float gset;
float fac,rsq,v1,v2;
if (iset == 0) {
do {
v1=2.0*ran1(idum)-1.0;
v2=2.0*ran1(idum)-1.0;
rsq=v1*v1+v2*v2;
} while (rsq >= 1.0 || rsq == 0.0);
fac=sqrt(-2.0*log(rsq)/rsq);
gset=v1*fac;
iset=1;
return v2*fac;
} else {
iset=0;
return gset;
}
}
/* (C) Copr. 1986-92 Numerical Recipes Software ]y.*/
here is my final ran2 code with the help of the board here:
Code:
/* random number between 0 and 1 using ran2 method p282*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#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)
/*Long period (>2e10^18) random number generator of L'Ecuyer with Bays-Durham shuffle
and added safeguards. Returns a random deviate between 0.0 and 1.0 (exlclusive of the endpoint values).
Call with idum a negative integer to initialize;
thereafter do not alter idum between successive deviates in a sequence.
RNMX should approxiamte the largest floating value that is less than 1. */
float ran2(long *idum);
int main()
{
float answer;
long *pointer; /* don't really need this; just pass &idum each time */
long idum = -time(0); /* get a value for initialization of ran2() */
int i;
pointer=&idum;
printf("*pointer = %ld\n", *pointer);
for (i = 0; i < 10; i++) {
answer=ran2(pointer);
printf ("%2d: %f, *pointer = %ld\n", i, answer, *pointer);
}
printf ("\nPress ENTER to continue.\n");
fflush (stdin);
(void) getchar();
return (0);
}
/*this is the ran2 function listed in the book, straight from the cd*/
float ran2(long *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;
}
#undef IM1
#undef IM2
#undef AM
#undef IMM1
#undef IA1
#undef IA2
#undef IQ1
#undef IQ2
#undef IR1
#undef IR2
#undef NTAB
#undef NDIV
#undef EPS
#undef RNMX
And here is my awful and messy attempt at patching the two together.... *cringes*
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
float gasdev(long *idum);
int main()
{
float gaussiannumber;
long *pointer;
long idum = answer;
int i;
pointer=&idum;
printf("*pointer = %1d\n", *pointer);
for (i = 0; i < 10; i++)
{
gaussiannumber = ran2(pointer);
printf ("%2d: %f, *pointer = %ld\n", i, gaussiannumber, *pointer);
}
printf ("\nPress ENTER to continue.\n");
fflush (stdin);
(void) getchar();
return (0);
}
{
float ran2(long *idum);
static int iset=0;
static float gaussiannumber;
float fac,rsq,v1,v2;
if (iset == 0) {
do {
v1=2.0*ran2(idum)-1.0;
v2=2.0*ran2(idum)-1.0;
rsq=v1*v1+v2*v2;
} while (rsq >= 1.0 || rsq == 0.0);
fac=sqrt(-2.0*log(rsq)/rsq);
gaussiannumber=v1*fac;
iset=1;
return v2*fac;
} else {
iset=0;
return gaussiannumber;
}
}
Am sorry about posting so much code, just want to make everything clear.
Any help is much appreciated- im not very good at this logical stuff... or at C programming in general (have been doing it for 2wks! just got "thrown" at me by my professor)
*smallest violin*