Creating a more random int?

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

    Creating a more random int?

    Hi all,

    I need to retrieve an integer from within a range ... this works ... below
    is my out puts ... it just does not seem so random ...

    Is there perhaps a suggestion out there to create a more random int ...?
    [color=blue][color=green][color=darkred]
    >>> random.randint( 3, 8)[/color][/color][/color]
    7[color=blue][color=green][color=darkred]
    >>> random.randint( 3, 8)[/color][/color][/color]
    3[color=blue][color=green][color=darkred]
    >>> random.randint( 3, 8)[/color][/color][/color]
    3[color=blue][color=green][color=darkred]
    >>> random.randint( 3, 8)[/color][/color][/color]
    4[color=blue][color=green][color=darkred]
    >>> random.randint( 3, 8)[/color][/color][/color]
    3[color=blue][color=green][color=darkred]
    >>> random.randint( 3, 8)[/color][/color][/color]
    4[color=blue][color=green][color=darkred]
    >>> random.randint( 3, 8)[/color][/color][/color]
    7[color=blue][color=green][color=darkred]
    >>> random.randint( 3, 8)[/color][/color][/color]
    7[color=blue][color=green][color=darkred]
    >>> random.randint( 3, 8)[/color][/color][/color]
    7[color=blue][color=green][color=darkred]
    >>> random.randint( 3, 8)[/color][/color][/color]
    5[color=blue][color=green][color=darkred]
    >>> random.randint( 3, 8)[/color][/color][/color]
    6[color=blue][color=green][color=darkred]
    >>> random.randint( 3, 8)[/color][/color][/color]
    3[color=blue][color=green][color=darkred]
    >>> random.randint( 3, 8)[/color][/color][/color]
    8[color=blue][color=green][color=darkred]
    >>> random.randint( 3, 8)[/color][/color][/color]
    8[color=blue][color=green][color=darkred]
    >>> random.randint( 3, 8)[/color][/color][/color]
    5[color=blue][color=green][color=darkred]
    >>> random.randint( 3, 8)[/color][/color][/color]
    3[color=blue][color=green][color=darkred]
    >>> random.randint( 3, 8)[/color][/color][/color]
    4

    Regards,


    Steven

  • Paul Rubin

    #2
    Re: Creating a more random int?

    "Steven Macintyre" <steven@steven. macintyre.name> writes:[color=blue]
    > I need to retrieve an integer from within a range ... this works ... below
    > is my out puts ... it just does not seem so random ...[/color]

    It's pretty normal for random small ints to not look random. Are
    there some statistical tests that the prng is definitely failing?
    [color=blue]
    > Is there perhaps a suggestion out there to create a more random int ...?[/color]

    Try reading bytes from os.urandom and converting them to ints in your
    program.

    Comment

    • mensanator@aol.com

      #3
      Re: Creating a more random int?


      Steven Macintyre wrote:[color=blue]
      > Hi all,
      >
      > I need to retrieve an integer from within a range ... this works ... below
      > is my out puts ... it just does not seem so random ...[/color]

      What's wrong with it?
      [color=blue]
      >
      > Is there perhaps a suggestion out there to create a more random int ...?[/color]

      What do you think the output should be?

      What's your criteria for "more random"?
      [color=blue]
      >[color=green][color=darkred]
      > >>> random.randint( 3, 8)[/color][/color]
      > 7[color=green][color=darkred]
      > >>> random.randint( 3, 8)[/color][/color]
      > 3[color=green][color=darkred]
      > >>> random.randint( 3, 8)[/color][/color]
      > 3[color=green][color=darkred]
      > >>> random.randint( 3, 8)[/color][/color]
      > 4[color=green][color=darkred]
      > >>> random.randint( 3, 8)[/color][/color]
      > 3[color=green][color=darkred]
      > >>> random.randint( 3, 8)[/color][/color]
      > 4[color=green][color=darkred]
      > >>> random.randint( 3, 8)[/color][/color]
      > 7[color=green][color=darkred]
      > >>> random.randint( 3, 8)[/color][/color]
      > 7[color=green][color=darkred]
      > >>> random.randint( 3, 8)[/color][/color]
      > 7[color=green][color=darkred]
      > >>> random.randint( 3, 8)[/color][/color]
      > 5[color=green][color=darkred]
      > >>> random.randint( 3, 8)[/color][/color]
      > 6[color=green][color=darkred]
      > >>> random.randint( 3, 8)[/color][/color]
      > 3[color=green][color=darkred]
      > >>> random.randint( 3, 8)[/color][/color]
      > 8[color=green][color=darkred]
      > >>> random.randint( 3, 8)[/color][/color]
      > 8[color=green][color=darkred]
      > >>> random.randint( 3, 8)[/color][/color]
      > 5[color=green][color=darkred]
      > >>> random.randint( 3, 8)[/color][/color]
      > 3[color=green][color=darkred]
      > >>> random.randint( 3, 8)[/color][/color]
      > 4
      >
      > Regards,
      >
      >
      > Steven[/color]

      Comment

      • Steven D'Aprano

        #4
        Re: Creating a more random int?

        On Tue, 24 Jan 2006 18:47:01 +0200, Steven Macintyre wrote:
        [color=blue]
        > Hi all,
        >
        > I need to retrieve an integer from within a range ... this works ... below
        > is my out puts ... it just does not seem so random ...[/color]

        You are choosing from six values. You should expect each value _about_
        17% of the time, in the long-term. On your tiny sample of 17 samples, you
        get this:

        7 3 3 4 3 4 7 7 7 5 6 3 8 8 5 3 4

        3 occurs five times or 29%
        4 occurs three times or 18%
        5 occurs two times or 12%
        6 occurs one time or 6%
        7 occurs four times or 23%
        8 occurs two times or 12%

        Looks pretty random to me.

        With only 17 trials, it would be a major miracle to get near 17% for every
        number. If you actually got something like this:

        5 3 4 5 8 4 7 5 6 8 6 3 6 7 4 3 7

        the odds are VERY strong that it is not really random. For one thing, it
        is too good to be true. For another thing, there are no repeated values.
        The chances of there being no repeated values is very small indeed.
        [color=blue]
        > Is there perhaps a suggestion out there to create a more random int ...?[/color]

        23 is the most random int. Perhaps you should use that.

        But all joking aside, the random number generator used by Python is one of
        the best in the world. What is at fault is your intuition about what
        random numbers should look like, not the random number generator.


        --
        Steven.

        Comment

        • Steven D'Aprano

          #5
          Re: Creating a more random int?

          On Tue, 24 Jan 2006 09:36:49 -0800, Paul Rubin wrote:
          [color=blue]
          > Try reading bytes from os.urandom and converting them to ints in your
          > program.[/color]

          He'll have the same problem with them, since the problem isn't the quality
          of the randomness, but his intuition of what a random sequence should look
          like.


          --
          Steven.

          Comment

          • Magnus Lycka

            #6
            Re: Creating a more random int?

            Steven D'Aprano wrote:[color=blue]
            > But all joking aside, the random number generator used by Python is one of
            > the best in the world. What is at fault is your intuition about what
            > random numbers should look like, not the random number generator.[/color]

            This is a well known problem, and there are methods to detect
            "too good to really be random" results in both research and
            accounting, based on statistical analysis of the numbers.

            The only thing the original poster really needs to understand
            is that chance has no memory. If you roll a dice, and it's not
            loaded, the chance that it will show a particular value is
            always one in six. All values are as likely. It doesn't matter
            that you've rolled 6 five times in a row. The change that the
            dice will come up with a 6 a 6th time is is still 1/6. I.e.
            it's as likely as any other particular value. Ten sixes in a
            row is just as likely as any other sequence of numbers. The
            chance for any sequence to occur is 6**-10.

            Die rolls are not like drawing cards from a deck of cards, where
            there are fewer queens left once we've drawn a queen.


            /Magnus

            P.S. Since I was a kid, I've heard people say: So you're born
            on new years day--how unusual. Sigh... We humans have a very
            good ability to spot things that seem to stick out from the
            ordinary, such as dates that look particular, or the number
            7 popping up thrice in a row. We also need to realize that,
            while these things that stick out help us find significant
            things, there are also a lot of "false positives"...

            Comment

            • Grant Edwards

              #7
              Re: Creating a more random int?

              On 2006-01-25, Magnus Lycka <lycka@carmen.s e> wrote:
              [color=blue]
              > P.S. Since I was a kid, I've heard people say: So you're born
              > on new years day--how unusual.[/color]

              Well, it happens to slightly less than 1/365th of the
              population[1], so it is rather unusual. Of course it's no more
              unusal that being born on June 19th or November 3rd or any
              other date you choose...

              [1] Assuming birth dates are uniformly distributed. Which the
              probably aren't in countries where a significant portion of
              the population schedules births to fit the hospital/doctors
              schedule.

              --
              Grant

              Comment

              • Dave Hansen

                #8
                Re: Creating a more random int?

                On Wed, 25 Jan 2006 15:21:43 -0000 in comp.lang.pytho n, Grant Edwards
                <grante@visi.co m> wrote:
                [color=blue]
                >On 2006-01-25, Magnus Lycka <lycka@carmen.s e> wrote:
                >[color=green]
                >> P.S. Since I was a kid, I've heard people say: So you're born
                >> on new years day--how unusual.[/color]
                >
                >Well, it happens to slightly less than 1/365th of the
                >population[1], so it is rather unusual. Of course it's no more
                >unusal that being born on June 19th or November 3rd or any
                >other date you choose...
                >
                >[1] Assuming birth dates are uniformly distributed. Which the
                > probably aren't in countries where a significant portion of
                > the population schedules births to fit the hospital/doctors
                > schedule.[/color]

                Google "birthday paradox" for a little OT fun.

                Regards,
                -=Dave

                --
                Change is inevitable, progress is not.

                Comment

                • Fredrik Lundh

                  #9
                  Re: Creating a more random int?

                  Grant Edwards wrote:
                  [color=blue]
                  > [1] Assuming birth dates are uniformly distributed. Which the
                  > probably aren't in countries where a significant portion of
                  > the population schedules births to fit the hospital/doctors
                  > schedule.[/color]

                  or in countries that use "nominal birth dates" for people born in
                  poor parts of the country...

                  </F>



                  Comment

                  • Magnus Lycka

                    #10
                    Re: Creating a more random int?

                    Grant Edwards wrote:[color=blue]
                    > Well, it happens to slightly less than 1/365th of the
                    > population[1], so it is rather unusual. Of course it's no more
                    > unusal that being born on June 19th or November 3rd or any
                    > other date you choose...[/color]

                    Exactly. And I've never heard anyone say to my sons that it's
                    so unusual that they are born on Jan 30th or June 27th!

                    Comment

                    • Tim Chase

                      #11
                      Re: Creating a more random int?

                      > Exactly. And I've never heard anyone say to my sons that it's[color=blue]
                      > so unusual that they are born on Jan 30th or June 27th![/color]

                      Now those born on Feb 29th...they're about a quarter as frequent :)

                      -tkc





                      Comment

                      • davbrow@gmail.com

                        #12
                        Re: Creating a more random int?

                        Well it IS unusual actually (~1/365.25). Please tell them for me.

                        -- Dave

                        Comment

                        • davbrow@gmail.com

                          #13
                          Re: Creating a more random int?

                          Steven,

                          Have you considered you might want a less random distribution? If you
                          just want to remove repeats so it 'seems' more random you could do that
                          (replace any repeat with another random value not equal to the last
                          value). It likely will not be as uniformly random as the original
                          sequence but it will be 'smoother' in some definition that you might
                          prefer for your uses. Be very careful though if you are going to use
                          it for technical work that requires a uniform distribution in the
                          formal sense.

                          -- Dave

                          Comment

                          • Grant Edwards

                            #14
                            Re: Creating a more random int?

                            On 2006-01-25, Fredrik Lundh <fredrik@python ware.com> wrote:[color=blue]
                            > Grant Edwards wrote:
                            >[color=green]
                            >> [1] Assuming birth dates are uniformly distributed. Which the
                            >> probably aren't in countries where a significant portion of
                            >> the population schedules births to fit the hospital/doctors
                            >> schedule.[/color]
                            >
                            > or in countries that use "nominal birth dates" for people born in
                            > poor parts of the country...[/color]

                            FWIW, "nominal brith dates" is a phrase with exactly one hit in
                            google. :)

                            --
                            Grant Edwards
                            grante@visi.com

                            Comment

                            • Fredrik Lundh

                              #15
                              Re: Creating a more random int?

                              Grant Edwards wrote:
                              [color=blue][color=green]
                              > > Grant Edwards wrote:
                              > >[color=darkred]
                              > >> [1] Assuming birth dates are uniformly distributed. Which the
                              > >> probably aren't in countries where a significant portion of
                              > >> the population schedules births to fit the hospital/doctors
                              > >> schedule.[/color]
                              > >
                              > > or in countries that use "nominal birth dates" for people born in
                              > > poor parts of the country...[/color]
                              >
                              > FWIW, "nominal brith dates" is a phrase with exactly one hit in
                              > google. :)[/color]

                              try "nominal birth date", and you'll find lots of references to Saddam,
                              Jesus, and fish, and a few pages that actually explain the concept...

                              </F>



                              Comment

                              Working...