ran3 random num generator

N

nkomli

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
 
N

nkomli

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.
 
N

nkomli

How about this?

http://www.cs.utah.edu/~tch/classes/cs4550/code/Numerical-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 -= ma[1+(i+30) % 55];
if (ma < MZ) ma += 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;

}
 
O

osmium

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?

I assume the initial value of the thing known in ran3 as idum is the seed.
After the initial call, don't *you* change it, bequeath it's management to
ran3. .

Any mistakes below?

<snip>

I have no idea. You asked for code and AFAIK got it. Why are you typing
all this stuff? (Rhetorical question)
 
O

ozgun.harmanci

Hello,
I think there is a mistake when setting mj in initialization code:
mj=MSEED-(*idum < 0 ? -*idum : *idum);

if absolute idum is set to something big, mj turns out negative and i
think this is a bug relevant to how ran3 works. The implementation of
ran3 in the numerical recipes book i have fixes this with labs:

mj=labs(MSEED-labs(*idum));

What do you think?

Actually I also have a question, anyone knows the period of ran3?

Thanks.
Arif.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top