ran3 random num generator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nkomli@gmail.com

    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
  • nkomli@gmail.com

    #2
    Re: ran3 random num generator

    On May 5, 1:34 am, nko...@gmail.co m 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.

    Comment

    • osmium

      #3
      Re: ran3 random num generator

      <nkomli@gmail.c omwrote:
      >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?



      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.


      Comment

      • nkomli@gmail.com

        #4
        Re: ran3 random num generator

        On May 5, 6:40 am, "osmium" <r124c4u...@com cast.netwrote:
        <nko...@gmail.c omwrote:
        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;

        }







        Comment

        • osmium

          #5
          Re: ran3 random num generator

          <nkomli@gmail.c omwrote:

          <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)


          Comment

          • ozgun.harmanci

            #6
            Re: ran3 random num generator

            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...@com cast.netwrote:
            <nko...@gmail.c omwrote:
            >
            <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)

            Comment

            Working...