simple PRNG?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Noob

    #46
    Re: simple PRNG?

    George Marsaglia wrote:
    [snip PRNG code and insightful comments]
    It's great to have you post to this group again.

    Regards.

    Comment

    • Chris Torek

      #47
      Re: simple PRNG?

      In article <rtGdnf5giO88in janZ2dnUVZ_tSkn Z2d@comcast.com >
      George Marsaglia <geo@stat.fsu.e duwrote:
      >/* initialize with any 32-bit seed x and any 32-bit y not 0 */
      >
      >static unsigned long x=2282008, y=362436069;
      >
      >#define sK ( x=69069*x+123, y^=y<<13, y^=y>>17, y^=y<<5, x+y )
      [snippage]
      >Use of sK in any expression will produce a random 32-bit integer,
      >(unsigned), so that, for example, u=sK*2.32830643 7e-10 will
      >produce a uniform real u in the interval [0,1) ...
      If "unsigned long" is 64 bits (and it is on a number of systems),
      this will produce a 64-bit result instead of a 32-bit result. I
      think[%] -- but have not attempted to prove -- that it is OK simply
      to reduce the result here mod 2^32 with:

      #define sK \
      ((x=69069*x+123 , y^=y<<13, y^=y>>17, y^=y<<5, x+y) & 0xffffffffUL)

      The mask (against 0xffffffffUL) should be optimized out by any good
      compiler in which "unsigned long" is already 32 bits. It is
      necessary if you expect the result to be limited to no more than
      32 bits, though.

      [% I am not sure whether extra retained bits in x and/or y are a
      problem. If they are, add more masks with 0xffffffffUL. Again,
      the generated code should be the same wherever the masks are a
      no-op, as on any machine with 32-bit "unsigned long". Alternatively,
      one could use "uint32_t" on C99 systems, but masking is completely
      portable, even to C89-only systems.]
      --
      In-Real-Life: Chris Torek, Wind River Systems
      Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
      email: gmail (figure it out) http://web.torek.net/torek/index.html

      Comment

      • Thad Smith

        #48
        Re: simple PRNG?

        Chris Torek wrote:
        In article <rtGdnf5giO88in janZ2dnUVZ_tSkn Z2d@comcast.com >
        George Marsaglia <geo@stat.fsu.e duwrote:
        >/* initialize with any 32-bit seed x and any 32-bit y not 0 */
        >>
        >static unsigned long x=2282008, y=362436069;
        >>
        >#define sK ( x=69069*x+123, y^=y<<13, y^=y>>17, y^=y<<5, x+y )
        [snippage]
        >Use of sK in any expression will produce a random 32-bit integer,
        >(unsigned), so that, for example, u=sK*2.32830643 7e-10 will
        >produce a uniform real u in the interval [0,1) ...
        >
        If "unsigned long" is 64 bits (and it is on a number of systems),
        this will produce a 64-bit result instead of a 32-bit result. I
        think[%] -- but have not attempted to prove -- that it is OK simply
        to reduce the result here mod 2^32 with:
        >
        #define sK \
        ((x=69069*x+123 , y^=y<<13, y^=y>>17, y^=y<<5, x+y) & 0xffffffffUL)
        The calculation of x should be OK, but y^=y>>17 would yield a different
        value. Try

        #define sK \
        (x=69069*x+123, y=(y^y<<13) & 0xffffffffUL, y^=y>>17, \
        y^=y<<5, (x+y) & 0xffffffffUL)
        [% I am not sure whether extra retained bits in x and/or y are a
        problem. If they are, add more masks with 0xffffffffUL. Again,
        the generated code should be the same wherever the masks are a
        no-op, as on any machine with 32-bit "unsigned long". Alternatively,
        one could use "uint32_t" on C99 systems, but masking is completely
        portable, even to C89-only systems.]
        Also, since uint32_t is not guaranteed to exist the masked approach is more
        general.

        --
        Thad

        Comment

        • pete

          #49
          Re: simple PRNG?

          Walter Roberson wrote:
          >
          In article <fs057r$7rd$1@a ioe.org>,
          jacob navia <jacob@nospam.o rgwrote:
          >
          unsigned long random(void)
          {
          register long x, hi, lo, t;

          /*
          * Compute x[n + 1] = (7^5 * x[n]) mod (2^31 - 1).
          * From "Random number generators:
          good ones are hard to find",
          * Park and Miller, Communications of the ACM,
          vol. 31, no. 10,
          * October 1988, p. 1195.
          */
          x = _randseed;
          hi = x / 127773;
          lo = x % 127773;
          t = 16807 * lo - 2836 * hi;
          if (t <= 0)
          t += 0x7fffffff;
          _randseed = t;
          return (t);
          }
          >
          It would be safer (or at least clearer) if most of your constants
          were suffixed with L to avoid their being interpreted (if only
          mentally) as overflowing the range of UINT_MAX on 16 bit systems.
          Why are there any signed type objects at all
          in that function definition?

          register long unsigned fixes everything.

          But I wouldn't publish purportedly portable code
          with the keyword "register" in it, either.

          --
          pete

          Comment

          • CBFalconer

            #50
            Re: simple PRNG?

            pete wrote:
            >
            .... snip ...
            >
            Why are there any signed type objects at all
            in that function definition?
            >
            register long unsigned fixes everything.
            >
            But I wouldn't publish purportedly portable code
            with the keyword "register" in it, either.
            Why not? register is still a reserved word. All it is guaranteed
            to do is prevent taking the address. Although it seems totally
            ridiculous here.

            --
            [mail]: Chuck F (cbfalconer at maineline dot net)
            [page]: <http://cbfalconer.home .att.net>
            Try the download section.



            --
            Posted via a free Usenet account from http://www.teranews.com

            Comment

            • pete

              #51
              Re: simple PRNG?

              CBFalconer wrote:
              >
              pete wrote:
              ... snip ...

              Why are there any signed type objects at all
              in that function definition?

              register long unsigned fixes everything.

              But I wouldn't publish purportedly portable code
              with the keyword "register" in it, either.
              >
              Why not?
              Because, ...
              register is still a reserved word. All it is guaranteed
              to do is prevent taking the address. Although it seems totally
              ridiculous here.
              .... I can't imagine that there ever might be
              a situation where I would want to prevent taking the address.

              --
              pete

              Comment

              • Walter Roberson

                #52
                Re: simple PRNG?

                In article <47EDA13E.4777@ mindspring.com> ,
                pete <pfiland@mindsp ring.comwrote:
                >CBFalconer wrote:
                >register is still a reserved word. All it is guaranteed
                >to do is prevent taking the address. Although it seems totally
                >ridiculous here.
                >... I can't imagine that there ever might be
                >a situation where I would want to prevent taking the address.
                It becomes an optimization hint to a compiler: if there are
                pointers in the code, the optimizer can know that none of them
                point to the object declared as 'register', which fact might
                allow it to use tighter code in some cases.
                --
                "Eightly percent of the people in the world are fools and the
                rest of us are in danger of contamination." -- Walter Matthau

                Comment

                • Keith Thompson

                  #53
                  Re: simple PRNG?

                  roberson@ibd.nr c-cnrc.gc.ca (Walter Roberson) writes:
                  In article <47EDA13E.4777@ mindspring.com> ,
                  pete <pfiland@mindsp ring.comwrote:
                  >>CBFalconer wrote:
                  >
                  >>register is still a reserved word. All it is guaranteed
                  >>to do is prevent taking the address. Although it seems totally
                  >>ridiculous here.
                  >
                  >>... I can't imagine that there ever might be
                  >>a situation where I would want to prevent taking the address.
                  >
                  It becomes an optimization hint to a compiler: if there are
                  pointers in the code, the optimizer can know that none of them
                  point to the object declared as 'register', which fact might
                  allow it to use tighter code in some cases.
                  On the other hand, since "register" can only be applied an object
                  declared inside a function, an optimizing compiler probably already
                  knows that the object's address is never taken.

                  On the other other hand, the code in question was published in 1988;
                  at the time, "register" might well have been useful as an optimization
                  hint.

                  --
                  Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                  Nokia
                  "We must do something. This is something. Therefore, we must do this."
                  -- Antony Jay and Jonathan Lynn, "Yes Minister"

                  Comment

                  • pete

                    #54
                    Re: simple PRNG?

                    Walter Roberson wrote:
                    >
                    In article <47EDA13E.4777@ mindspring.com> ,
                    pete <pfiland@mindsp ring.comwrote:
                    CBFalconer wrote:
                    >
                    register is still a reserved word. All it is guaranteed
                    to do is prevent taking the address. Although it seems totally
                    ridiculous here.
                    >
                    ... I can't imagine that there ever might be
                    a situation where I would want to prevent taking the address.
                    >
                    It becomes an optimization hint to a compiler: if there are
                    pointers in the code, the optimizer can know that none of them
                    point to the object declared as 'register', which fact might
                    allow it to use tighter code in some cases.
                    Is that something that you've done for that reason,
                    or is that an example of something that you can imagine
                    that I can't?

                    --
                    pete

                    Comment

                    • Richard Tobin

                      #55
                      Re: simple PRNG?

                      In article <fsk9qp$6i0$1@c anopus.cc.umani toba.ca>,
                      Walter Roberson <roberson@ibd.n rc-cnrc.gc.cawrote :
                      >It becomes an optimization hint to a compiler: if there are
                      >pointers in the code, the optimizer can know that none of them
                      >point to the object declared as 'register'
                      I would have thought most compilers would be able to tell whether
                      the address of a variable is taken, so that this hint doesn't
                      provide any information they don't already have.

                      -- Richard
                      --
                      :wq

                      Comment

                      • Richard

                        #56
                        Re: simple PRNG?

                        roberson@ibd.nr c-cnrc.gc.ca (Walter Roberson) writes:
                        In article <47EDA13E.4777@ mindspring.com> ,
                        pete <pfiland@mindsp ring.comwrote:
                        >>CBFalconer wrote:
                        >
                        >>register is still a reserved word. All it is guaranteed
                        >>to do is prevent taking the address. Although it seems totally
                        >>ridiculous here.
                        >
                        >>... I can't imagine that there ever might be
                        >>a situation where I would want to prevent taking the address.
                        >
                        It becomes an optimization hint to a compiler: if there are
                        pointers in the code, the optimizer can know that none of them
                        point to the object declared as 'register', which fact might
                        allow it to use tighter code in some cases.
                        Sounds strange.

                        Comment

                        Working...