Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: ran3 random...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 6 Topic 45756 of 47034
Post > Topic >>

Re: ran3 random num generator

by nkomli@[EMAIL PROTECTED] May 5, 2008 at 06:35 PM

On May 5, 6:40 am, "osmium" <r124c4u...@[EMAIL PROTECTED]
> wrote:
> <nko...@[EMAIL PROTECTED]
> wrote:
> >> 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/cl*****/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[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;

}
 




 6 Posts in Topic:
ran3 random num generator
nkomli@[EMAIL PROTECTED]   2008-05-05 01:34:39 
Re: ran3 random num generator
nkomli@[EMAIL PROTECTED]   2008-05-05 01:38:56 
Re: ran3 random num generator
"osmium" <r1  2008-05-05 06:40:52 
Re: ran3 random num generator
nkomli@[EMAIL PROTECTED]   2008-05-05 18:35:41 
Re: ran3 random num generator
"osmium" <r1  2008-05-05 20:13:33 
Re: ran3 random num generator
"ozgun.harmanci"  2008-05-08 12:18:46 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 2:56:43 CDT 2008.