generater big rand number

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

    generater big rand number

    i use g++ to generater rand number, now i find that the RAND_MAX is
    32367 in my computer, how can i make a bigger rand number( the number
    is wihin in the integer(2^32-1))
  • peter koch

    #2
    Re: generater big rand number

    On 19 Okt., 09:37, remlostime <remlost...@gma il.comwrote:
    i use g++ to generater rand number, now i find that the RAND_MAX is
    32367 in my computer, how can i make a bigger rand number( the number
    is wihin in the integer(2^32-1))
    You have several options: one is to use a library such as boost which
    gives you several options to choose different random number generators
    of high quality. Another is to compose your number by calling rand
    several times. To illustrate: if your random number only gave values
    from 0 to 9, but you needed values from 0 to 99, the solution would my
    to use rand()*10 + rand(). I am sure you can extrapolate from here ;-)

    /Peter

    Comment

    • Juha Nieminen

      #3
      Re: generater big rand number

      remlostime wrote:
      i use g++ to generater rand number, now i find that the RAND_MAX is
      32367 in my computer, how can i make a bigger rand number( the number
      is wihin in the integer(2^32-1))
      By using a better random number generator? There are tons of them if
      you search the internet.

      (Curiously it's actually surprisingly difficult to find a good random
      number generator which is portable, written in C++, very easy to use and
      doesn't require you to install the entire Boost library to simply use
      the RNG. For this reason I made this C++ version of the ISAAC rng:
      http://warp.povusers.org/IsaacRand.zip )

      Comment

      • gw7rib@aol.com

        #4
        Re: generater big rand number

        On 19 Oct, 10:43, peter koch <peter.koch.lar ...@gmail.comwr ote:
        On 19 Okt., 09:37, remlostime <remlost...@gma il.comwrote:
        >
        i use g++ to generater rand number, now i find that the RAND_MAX is
        32367 in my computer, how can i make a bigger rand number( the number
        is wihin in the integer(2^32-1))
        >
        You have several options: one is to use a library such as boost which
        gives you several options to choose different random number generators
        of high quality. Another is to compose your number by calling rand
        several times. To illustrate: if your random number only gave values
        from 0 to 9, but you needed values from 0 to 99, the solution would my
        to use rand()*10 + rand(). I am sure you can extrapolate from here ;-)
        I don't think your second approach will work. For example, suppose
        your random number generator gives only ten values and produces the
        sequence:

        1, 6, 4, 7, 9, 0, 3, 5, 2, 8, 1, 6, etc

        then your improved version will only ever give the numbers 16, 64, 47,
        79, 90, 3, 35, 52, 28 and 81, instead of the full range from 0 to 99.

        Comment

        • blargg

          #5
          Re: generater big rand number

          In article <9LDKk.48$UW4.1 3@read4.inet.fi >, Juha Nieminen
          <nospam@thanks. invalidwrote:
          (Curiously it's actually surprisingly difficult to find a good random
          number generator which is portable, written in C++, very easy to use and
          doesn't require you to install the entire Boost library to simply use
          the RNG. For this reason I made this C++ version of the ISAAC rng:
          http://warp.povusers.org/IsaacRand.zip )
          Agreed; that one seems to assume that unsigned int has exactly 32 bits, no
          more, no less. Take a look at the ind macro in IsaacRand.cc line 21.
          That's just one non-portability I found after a quick scan.

          Comment

          • peter koch

            #6
            Re: generater big rand number

            On 19 Okt., 22:03, gw7...@aol.com wrote:
            On 19 Oct, 10:43, peter koch <peter.koch.lar ...@gmail.comwr ote:
            >
            On 19 Okt., 09:37, remlostime <remlost...@gma il.comwrote:
            >
            i use g++ to generater rand number, now i find that the RAND_MAX is
            32367 in my computer, how can i make a bigger rand number( the number
            is wihin in the integer(2^32-1))
            >
            You have several options: one is to use a library such as boost which
            gives you several options to choose different random number generators
            of high quality. Another is to compose your number by calling rand
            several times. To illustrate: if your random number only gave values
            from 0 to 9, but you needed values from 0 to 99, the solution would my
            to use rand()*10 + rand(). I am sure you can extrapolate from here ;-)
            >
            I don't think your second approach will work. For example, suppose
            your random number generator gives only ten values and produces the
            sequence:
            >
            1, 6, 4, 7, 9, 0, 3, 5, 2, 8, 1, 6, etc
            >
            then your improved version will only ever give the numbers 16, 64, 47,
            79, 90, 3, 35, 52, 28 and 81, instead of the full range from 0 to 99.
            That would be a short sequence - not anything you'd expect from a
            quality random generator.

            /Peter

            Comment

            • Pete Becker

              #7
              Re: generater big rand number

              On 2008-10-19 16:03:39 -0400, gw7rib@aol.com said:
              On 19 Oct, 10:43, peter koch <peter.koch.lar ...@gmail.comwr ote:
              >On 19 Okt., 09:37, remlostime <remlost...@gma il.comwrote:
              >>
              >>i use g++ to generater rand number, now i find that the RAND_MAX is
              >>32367 in my computer, how can i make a bigger rand number( the number
              >>is wihin in the integer(2^32-1))
              >>
              >You have several options: one is to use a library such as boost which
              >gives you several options to choose different random number generators
              >of high quality. Another is to compose your number by calling rand
              >several times. To illustrate: if your random number only gave values
              >from 0 to 9, but you needed values from 0 to 99, the solution would my
              >to use rand()*10 + rand(). I am sure you can extrapolate from here ;-)
              >
              I don't think your second approach will work. For example, suppose
              your random number generator gives only ten values and produces the
              sequence:
              >
              1, 6, 4, 7, 9, 0, 3, 5, 2, 8, 1, 6, etc
              >
              then your improved version will only ever give the numbers 16, 64, 47,
              79, 90, 3, 35, 52, 28 and 81, instead of the full range from 0 to 99.
              Well, yes, if the generator produces a sequence of length ten, then
              there's not much you can do. But producing random digits from 0 to 9
              doesn't mean producing a sequence of length ten. Typically a random
              number generator has a much longer period than that. If the period is
              long enough, there's no problem tiling the values, as the second
              approach suggests.

              --
              Pete
              Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
              Standard C++ Library Extensions: a Tutorial and Reference
              (www.petebecker.com/tr1book)

              Comment

              • James Kanze

                #8
                Re: generater big rand number

                On Oct 19, 11:26 pm, Pete Becker <p...@versatile coding.comwrote :
                On 2008-10-19 16:03:39 -0400, gw7...@aol.com said:
                On 19 Oct, 10:43, peter koch <peter.koch.lar ...@gmail.comwr ote:
                On 19 Okt., 09:37, remlostime <remlost...@gma il.comwrote:
                >i use g++ to generater rand number, now i find that the
                >RAND_MAX is 32367 in my computer, how can i make a bigger
                >rand number( the number is wihin in the integer(2^32-1))
                You have several options: one is to use a library such as
                boost which gives you several options to choose different
                random number generators of high quality. Another is to
                compose your number by calling rand several times. To
                illustrate: if your random number only gave values from 0
                to 9, but you needed values from 0 to 99, the solution
                would my to use rand()*10 + rand(). I am sure you can
                extrapolate from here ;-)
                I don't think your second approach will work. For example,
                suppose your random number generator gives only ten values
                and produces the sequence:
                1, 6, 4, 7, 9, 0, 3, 5, 2, 8, 1, 6, etc
                then your improved version will only ever give the numbers
                16, 64, 47, 79, 90, 3, 35, 52, 28 and 81, instead of the
                full range from 0 to 99.
                Well, yes, if the generator produces a sequence of length ten,
                then there's not much you can do. But producing random digits
                from 0 to 9 doesn't mean producing a sequence of length ten.
                Typically a random number generator has a much longer period
                than that. If the period is long enough, there's no problem
                tiling the values, as the second approach suggests.
                Yes and no. The sequence should be significantly longer than
                the value one is trying to generate; in other words, the value
                actually returned by rand() shouldn't represent the entire
                internal state of the machine.

                For historical reasons, at in some environments, RAND_MAX is
                defined as 32767, even though the actual generator uses 31 or 32
                bits internally (and has an actual period of around 2^31). If
                this is the case, then the proposed technique is fine.
                Similarly, rand() is required to return an int---a number of
                quality generators use significantly more state internally. In
                such cases, the technique is also valid. If the generator's
                period is only RAND_MAX, however, it's likely to be too pretty
                bad (but depending on the use, maybe "good enough" anyway).

                --
                James Kanze (GABI Software) email:james.kan ze@gmail.com
                Conseils en informatique orientée objet/
                Beratung in objektorientier ter Datenverarbeitu ng
                9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                Comment

                • Juha Nieminen

                  #9
                  Re: generater big rand number

                  blargg wrote:
                  In article <9LDKk.48$UW4.1 3@read4.inet.fi >, Juha Nieminen
                  <nospam@thanks. invalidwrote:
                  >
                  > (Curiously it's actually surprisingly difficult to find a good random
                  >number generator which is portable, written in C++, very easy to use and
                  >doesn't require you to install the entire Boost library to simply use
                  >the RNG. For this reason I made this C++ version of the ISAAC rng:
                  > http://warp.povusers.org/IsaacRand.zip )
                  >
                  Agreed; that one seems to assume that unsigned int has exactly 32 bits, no
                  more, no less. Take a look at the ind macro in IsaacRand.cc line 21.
                  That's just one non-portability I found after a quick scan.
                  When I say "portable" I mean things like "doesn't include <windows.h>"
                  and the like. I have seen *way* too many RNGs out there which use
                  whatever non-standard headers and non-standard code.

                  If you are worried that in some system 'int' might not be 32-bit then
                  put some assert() somewhere or whatever. The actual RNG code is not
                  mine. I only wrapped it inside the class and removed everything from the
                  global namespace (the original C code put something like 100 symbols in
                  the global namespace). It fulfills my needs for a fast high-quality RNG
                  in both linux and windows just fine. The same cannot be said from the
                  majority of RNG libraries I have seen.

                  Comment

                  Working...