Re: RNGs: A Super KISS

Discussion in 'C++' started by user923005, Nov 3, 2009.

  1. user923005

    user923005 Guest

    On Nov 3, 7:46 am, geo <> wrote:
    > /*
    > For those mesmerized (or Mersenne-ized?) by a RNG
    > with period 2^19937-1, I offer one here with period
    > 54767*2^1337279---over 10^396564 times as long.
    > It is one of my CMWC (Complimentary-Multiply-With-Carry) RNGs,
    > and is suggested here as one of the components of a
    > super-long-period KISS (Keep-It-Simple-Stupid) RNG.
    >
    > With b=2^32 and a=7010176, and given a 32-bit x, and a 32-bit c, this
    > generator produces a new x,c by forming 64-bit t=a*x+c then replacing:
    > c=top 32 bits of t and x=(b-1)-(bottom 32 bits of t). In C: c=t>>32;
    > x=~t;
    >
    > For many years, CPUs have had machine instructions to form such a
    > 64-bit t and extract the top and bottom halves, but unfortunately
    > only recent Fortran versions have means to easily invoke them.
    >
    > Ability to do those extractions leads to implementations that are
    > simple
    > and extremely fast---some 140 million per second on my desktop PC.
    >
    > Used alone, this generator passes all the Diehard Battery of Tests,
    > but
    > its simplicity makes it well-suited to serve as one of the three
    > components
    > of a KISS RNG, based on the Keep-It-Simple-Stupid principle, and the
    > idea,
    > supported by both theory and practice, that the combination of RNGs
    > based on
    > different mathematical models can be no worse---and is usually
    > better---than
    > any of the components.
    >
    > So here is a complete C version of what might be called a SUPER KISS
    > RNG,
    > combining, by addition mod 2^32, a Congruential RNG, a Xorshift RNG
    > and the super-long-period CMWC RNG:
    > */
    >
    > #include <stdio.h>
    > static unsigned long Q
    > [41790],indx=41790,carry=362436,xcng=1236789,xs=521288629;
    >
    > #define CNG ( xcng=69609*xcng+123 )    /*Congruential*/
    > #define XS  ( xs^=xs<<13, xs^=(unsigned)xs>>17, xs^=xs>>5 )  /
    > *Xorshift*/
    > #define SUPR ( indx<41790 ? Q[indx++] : refill() )
    > #define KISS SUPR+CNG+XS
    >
    >   int refill( )
    >   { int i; unsigned long long t;
    >   for(i=0;i<41790;i++) { t=7010176LL*Q+carry; carry=(t>>32); Q=~
    > (t);}
    >   indx=1; return (Q[0]);
    >   }
    >
    > int main()
    > {unsigned long i,x;
    >  for(i=0;i<41790;i++) Q=CNG+XS;
    >  for(i=0;i<1000000000;i++) x=KISS;
    >  printf("     x=%d.\nDoes x=-872412446?\n",x);
    >
    > }
    >
    > /*
    > Running this program should produce 10^9 KISSes in some 7-15 seconds.
    > You are invited to cut, paste, compile and run for yourself, checking
    > to
    > see if the last value is as designated, (formatted as a signed integer
    > for
    > potential comparisons with systems using signed integers).
    > You may want to report or comment on implementations for other
    > languages.
    >
    > The arithmetic operations are suited for either signed or unsigned
    > integers.
    > Thus, with  (64-bit)t=a*x+c,  x=t%b in C or x=mod(t,b) in Fortran, and
    > c=c/b in either C or Fortran, but with ways to avoid integer
    > divisions,
    > and subsequent replacement of x by its base-b complement, ~x in C.
    >
    > With b=2^32 and p=54767*2^1337287+1, the SUPR part of this Super KISS
    > uses my CMWC method to produce, in reverse order, the base-b expansion
    > of k/p for some k determined by the values used to seed the Q array.
    > The period is the order of b for that prime p:
    >    54767*2^1337279, about 2^1337294 or 10^402566.
    > (It took a continuous run of 24+ days on an earlier PC to
    > establish that order.  My thanks to the wizards behind PFGW
    > and to Phil Carmody for some suggested code.)
    >
    > Even the Q's all zero, should seeding be overlooked in main(),
    > will still produce a sequence of the required period, but will
    > put the user in a strange and exceedingly rare place in the entire
    > sequence.  Users should choose a reasonable number of the 1337280
    > random bits that a fully-seeded  Q array requires.
    >
    > Using your own choices of merely 87 seed bits, 32 each for xcng,xs
    > and 23 for carry<7010176, then initializing the Q array with
    >             for(i=0;i<41790;i++) Q=CNG+XS;
    > should serve well for many applications, but others, such as in
    > Law or Gaming, where a minimum number of possible outcomes may be
    > required, might need more of the 1337280 seed bits for the Q array.
    >
    > As might applications in cryptography: With an unknown but fully-
    > seeded Q array, a particular string of, say, 41000 successive SUPR
    > values will appear at more than 2^20000 locations in the full
    > sequence,
    > making it virtually impossible to get the location of that particular
    > string in the full loop, and thus predict coming or earlier values,
    > even if able to undo the CNG+XS operations.
    > */
    >
    > /*
    > So I again invite you to cut, paste, compile and run the above C
    > program.
    > 1000 million KISSes should be generated, and the specified result
    > appear,
    > by the time you count slowly to fifteen.
    > (Without an optimizing compiler, you may have to count more slowly.)
    > */
    >
    > /* George Marsaglia */


    /*
    Here is a C++ version. The C version is quite a bit faster
    because there are no function calls at all.
    Can any of you C++ gurus bump the speed without losing encapsulation?
    I get about 5 seconds for the C version and about 8 seconds for the
    C++ version.

    -- d.corbit
    */

    #include <iostream>
    /*
    For those mesmerized (or Mersenne-ized?) by a RNG
    with period 2^19937-1, I offer one here with period
    54767*2^1337279---over 10^396564 times as long.
    It is one of my CMWC (Complimentary-Multiply-With-Carry) RNGs,
    and is suggested here as one of the components of a
    super-long-period KISS (Keep-It-Simple-Stupid) RNG.

    With b=2^32 and a=7010176, and given a 32-bit x, and a 32-bit c, this
    generator produces a new x,c by forming 64-bit t=a*x+c then replacing:
    c=top 32 bits of t and x=(b-1)-(bottom 32 bits of t). In C: c=t>>32;
    x=~t;


    For many years, CPUs have had machine instructions to form such a
    64-bit t and extract the top and bottom halves, but unfortunately
    only recent Fortran versions have means to easily invoke them.


    Ability to do those extractions leads to implementations that are
    simple
    and extremely fast---some 140 million per second on my desktop PC.


    Used alone, this generator passes all the Diehard Battery of Tests,
    but
    its simplicity makes it well-suited to serve as one of the three
    components
    of a KISS RNG, based on the Keep-It-Simple-Stupid principle, and the
    idea,
    supported by both theory and practice, that the combination of RNGs
    based on
    different mathematical models can be no worse---and is usually
    better---than
    any of the components.


    So here is a complete C version of what might be called a SUPER KISS
    RNG,
    combining, by addition mod 2^32, a Congruential RNG, a Xorshift RNG
    and the super-long-period CMWC RNG:
    */

    class SuperKiss {

    private:
    unsigned long Q[41790];
    unsigned long indx;
    unsigned long carry;
    unsigned long xcng;
    unsigned long xs;

    int refill ()
    {
    int i;
    unsigned long long t;
    for (i = 0; i < 41790; i++)
    {
    t = 7010176LL * Q + carry;
    carry = (t >> 32);
    Q = ~(t);
    }
    indx = 1;
    return (Q[0]);
    }

    public:
    // Constructor:
    SuperKiss()
    {
    indx = 41790;
    carry = 362436;
    xcng = 1236789;
    xs = 521288629;
    unsigned i;
    for (i = 0; i < 41790; i++)
    Q = (xcng = 69609 * xcng + 123) +
    (xs ^= xs << 13, xs ^= (unsigned) xs >> 17, xs ^=
    xs >> 5);
    }

    // Collect next random number:
    unsigned long SKRand() {
    return (indx < 41790 ? Q[indx++] : refill ()) +
    (xcng = 69609 * xcng + 123) +
    (xs ^= xs << 13, xs ^= (unsigned) xs >> 17, xs ^= xs >>
    5);
    }
    };

    int
    main ()
    {
    unsigned long i
    int x;
    SuperKiss sk;
    for (i = 0; i < 1000000000; i++)
    x = sk.SKRand();
    std::cout << " x = " << x << std::endl << "does Does
    x=-872412446?" << std::endl;
    return 0;
    }

    /*
    Running this program should produce 10^9 KISSes in some 7-15 seconds.
    You are invited to cut, paste, compile and run for yourself, checking
    to
    see if the last value is as designated, (formatted as a signed integer
    for
    potential comparisons with systems using signed integers).
    You may want to report or comment on implementations for other
    languages.

    The arithmetic operations are suited for either signed or unsigned
    integers.
    Thus, with (64-bit)t=a*x+c, x=t%b in C or x=mod(t,b) in Fortran, and
    c=c/b in either C or Fortran, but with ways to avoid integer
    divisions,
    and subsequent replacement of x by its base-b complement, ~x in C.


    With b=2^32 and p=54767*2^1337287+1, the SUPR part of this Super KISS
    uses my CMWC method to produce, in reverse order, the base-b expansion
    of k/p for some k determined by the values used to seed the Q array.
    The period is the order of b for that prime p:
    54767*2^1337279, about 2^1337294 or 10^402566.
    (It took a continuous run of 24+ days on an earlier PC to
    establish that order. My thanks to the wizards behind PFGW
    and to Phil Carmody for some suggested code.)


    Even the Q's all zero, should seeding be overlooked in main(),
    will still produce a sequence of the required period, but will
    put the user in a strange and exceedingly rare place in the entire
    sequence. Users should choose a reasonable number of the 1337280
    random bits that a fully-seeded Q array requires.


    Using your own choices of merely 87 seed bits, 32 each for xcng,xs
    and 23 for carry<7010176, then initializing the Q array with
    for(i=0;i<41790;i++) Q=CNG+XS;
    should serve well for many applications, but others, such as in
    Law or Gaming, where a minimum number of possible outcomes may be
    required, might need more of the 1337280 seed bits for the Q array.


    As might applications in cryptography: With an unknown but fully-
    seeded Q array, a particular string of, say, 41000 successive SUPR
    values will appear at more than 2^20000 locations in the full
    sequence,
    making it virtually impossible to get the location of that particular
    string in the full loop, and thus predict coming or earlier values,
    even if able to undo the CNG+XS operations.
    */


    /*
    So I again invite you to cut, paste, compile and run the above C
    program.
    1000 million KISSes should be generated, and the specified result
    appear,
    by the time you count slowly to fifteen.
    (Without an optimizing compiler, you may have to count more slowly.)
    */


    /* George Marsaglia */
    user923005, Nov 3, 2009
    #1
    1. Advertising

  2. user923005

    user923005 Guest

    On Nov 3, 12:23 pm, user923005 <> wrote:
    I copied and pasted from the wrong file. Here is the correct code
    [snip]
    class SuperKiss {

    private:
    unsigned long Q[41790];
    unsigned long indx;
    unsigned long carry;
    unsigned long xcng;
    unsigned long xs;

    int refill ()
    {
    int i;
    unsigned long long t;
    for (i = 0; i < 41790; i++)
    {
    t = 7010176LL * Q + carry;
    carry = (t >> 32);
    Q = ~(t);
    }
    indx = 1;
    return (Q[0]);
    }

    public:
    // Constructor:
    SuperKiss()
    {
    indx = 41790;
    carry = 362436;
    xcng = 1236789;
    xs = 521288629;
    unsigned i;
    for (i = 0; i < 41790; i++)
    Q = (xcng = 69609 * xcng + 123) +
    (xs ^= xs << 13, xs ^= (unsigned) xs >> 17, xs ^=
    xs >> 5);
    }

    // Collect next random number:
    unsigned long SKRand() {
    return (indx < 41790 ? Q[indx++] : refill ()) +
    (xcng = 69609 * xcng + 123) +
    (xs ^= xs << 13, xs ^= (unsigned) xs >> 17, xs ^= xs >>
    5);
    }
    };

    int
    main ()
    {
    unsigned long i;
    int x=0;
    SuperKiss sk;
    for (i = 0; i < 1000000000; i++)
    x = sk.SKRand();
    std::cout << " x = " << x << std::endl << "Does x=-872412446?"
    << std::endl;
    return 0;
    }
    user923005, Nov 3, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. geo

    64-bit KISS RNGs

    geo, Feb 28, 2009, in forum: C Programming
    Replies:
    23
    Views:
    2,937
  2. geo

    RNGs: A Super KISS

    geo, Nov 3, 2009, in forum: C Programming
    Replies:
    8
    Views:
    536
    user923005
    Nov 4, 2009
  3. user923005

    Re: RNGs: A Super KISS

    user923005, Nov 4, 2009, in forum: C++
    Replies:
    2
    Views:
    528
    Michael Doubez
    Nov 5, 2009
  4. geo

    RNGs: A double KISS

    geo, Apr 14, 2010, in forum: C Programming
    Replies:
    10
    Views:
    984
    steve
    Apr 17, 2010
  5. Dann Corbit

    Re: RNGs: A double KISS

    Dann Corbit, Apr 14, 2010, in forum: C++
    Replies:
    3
    Views:
    392
    Jonathan Lee
    Apr 14, 2010
Loading...

Share This Page