473,322 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

ran3 random num generator

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
Jun 27 '08 #1
5 7801
On May 5, 1:34 am, nko...@gmail.com 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.
Jun 27 '08 #2
<nk****@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/.../sources-used/

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.
Jun 27 '08 #3
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;

}



Jun 27 '08 #4
<nk****@gmail.comwrote:

<reorganized>
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)
Jun 27 '08 #5
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.

On May 5, 11:13 pm, "osmium" <r124c4u...@comcast.netwrote:
<nko...@gmail.comwrote:

<reorganized>
Am I right in guessing that I have to feed a seed
value when I callran3because 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 inran3asidumis the seed.
After the initial call, don't *you* change it, bequeath it's management toran3. .
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)
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Brandon Michael Moore | last post by:
I'm trying to test a web application using a tool written in python. I would like to be able to generate random values to put in fields. I would like to be able to generate random dates (in a...
28
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister or Wichmann-Hill generators. Comments are...
3
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I...
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
13
by: porterboy76 | last post by:
If you only use a 32 bit seed for a random number generator, does that mean you can only ever produce a maximum of 2^32 (approx 4 billion) different sequences? What about the Mersenne Twister,...
2
by: Matthew Wilson | last post by:
The random.jumpahead documentation says this: Changed in version 2.3: Instead of jumping to a specific state, n steps ahead, jumpahead(n) jumps to another state likely to be separated by many...
3
by: Daniel | last post by:
Hey guys Using Random(), how random is it, is it possible to be predicted? I have a requirement for a very good random number generator. I was doing something such as: Random randomSeed = new...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.