On May 5, 6:40 am, "osmium" <r124c4u...@comcast.netwrote:
<nko...@gmail.comwrote:
Does anyone know where I can find a version of the ran3 random number
generator for C++ that will play nice on windows as a console app,
preferably something you can call simply with random1 =ran3() or
something similar.
All the versions I found so far are implemented in different languages
or for some reason only work with Linux.
Thanks
Sorry, I forgot to mention that the ran3 generator algorithm is the
one mentioned in the book Numerical recipes.
How about this?
http://www.cs.utah.edu/~tch/classes/...al-Recipes/sou...
I changed the call to this:
float ran3(int* idum)
//int *idum;
and it seemed to work. Tested with DevC (in C++ mode) on a Windows console
application.
Any mistakes below? Am I right in guessing that I have to feed a seed
value when I call ran3 because it doesn't generate its own? If so how
would I set up the seed to generate a sequence of 'random' numbers in
a loop?
#include <iostream// for std::cout
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include <cstdio>
#include <time.h>
#include <math.h>
using namespace std;
#define MBIG 1000000000
#define MSEED 161803398
#define MZ 0
#define FAC (1.0/MBIG)
float ran3(int *) ;
float ran3(int* idum)
//int *idum;
{
static int inext,inextp;
static long ma[56];
static int iff=0;
long mj,mk;
int i,ii,k;
if (*idum < 0 || iff == 0) {
iff=1;
mj=MSEED-(*idum < 0 ? -*idum : *idum);
mj %= MBIG;
ma[55]=mj;
mk=1;
for (i=1;i<=54;i++) {
ii=(21*i) % 55;
ma[ii]=mk;
mk=mj-mk;
if (mk < MZ) mk += MBIG;
mj=ma[ii];
}
for (k=1;k<=4;k++)
for (i=1;i<=55;i++) {
ma[i] -= ma[1+(i+30) % 55];
if (ma[i] < MZ) ma[i] += MBIG;
}
inext=0;
inextp=31;
*idum=1;
}
if (++inext == 56) inext=1;
if (++inextp == 56) inextp=1;
mj=ma[inext]-ma[inextp];
if (mj < MZ) mj += MBIG;
ma[inext]=mj;
return mj*FAC;
}
#undef MBIG
#undef MSEED
#undef MZ
#undef FAC
int main()
{
double random;
int seed =2;
random =ran3(&seed);
cout << random << endl;
return 0;
}