Random Number Generation?

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

    #1

    Random Number Generation?

    Hello All,

    I need some help with random number generation. What I
    need exactly is:

    To create a few thousand numbers, decimal and
    integers, between 5 and 90,
    and then to export them as a single column at a
    spreadsheet.

    I am newbie, I was not able to create decimals with
    the random modules of
    Python 2.3.

    Thanks, Dimos


    _______________ _______________ _______________ _____
    Do You Yahoo!?
    Tired of spam? Yahoo! Mail has the best spam protection around
    Yahoo Mail: Your smarter, faster, free email solution. Organize your inbox, protect your privacy, and tackle tasks efficiently with AI-powered features and robust security tools.

  • Alex Martelli

    #2
    Re: Random Number Generation?

    Dimos <dimos_anastasi ou@yahoo.com> wrote:
    [color=blue]
    > Hello All,
    >
    > I need some help with random number generation. What I
    > need exactly is:
    >
    > To create a few thousand numbers, decimal and
    > integers, between 5 and 90,
    > and then to export them as a single column at a
    > spreadsheet.
    >
    > I am newbie, I was not able to create decimals with
    > the random modules of Python 2.3.[/color]

    The random module lets you create floats and integers, not instances of
    class decimal.Decimal (surely not in 2.3, which didn't even HAVE a
    module decimal in its standard library!). If you want floats, the way
    to create a float with uniform distribution between 5 and 90 is to call
    random.uniform( 5, 90). If you want 3000:

    r3k = [random.uniform( 5, 90) for i in xrange(3000)]

    None of the 3k numbers will be integers, and it's unlikely that any of
    the 3k floats happens to have a fractional part that's exactly 0. If
    you do want integers as well as floats, you'll have to decide with what
    probability an integer must appear instead of a float, and do a second
    pass on r3k to enforce this.


    Alex

    Comment

    • mensanator@aol.com

      #3
      Re: Random Number Generation?


      Dimos wrote:[color=blue]
      > Hello All,
      >
      > I need some help with random number generation. What I
      > need exactly is:
      >
      > To create a few thousand numbers, decimal and
      > integers, between 5 and 90,
      > and then to export them as a single column at a
      > spreadsheet.
      >
      > I am newbie, I was not able to create decimals with
      > the random modules of
      > Python 2.3.[/color]

      You use randint(a,b) to generate an integer between a and b.

      For real numbers, the function is random(). But that result is
      always between 0.0 and 1.0, so you have to make the range
      adjustment yourself.
      [color=blue][color=green][color=darkred]
      >>> import random
      >>> for i in range(10):[/color][/color][/color]
      print random.random() *85 + 5

      20.2844473176
      83.5690712033
      77.3459998722
      8.79906993754
      53.3672450881
      25.2609744882
      19.8894951301
      39.9794852838
      43.4056977237
      21.7770662903


      [color=blue]
      >
      > Thanks, Dimos
      >
      >
      > _______________ _______________ _______________ _____
      > Do You Yahoo!?
      > Tired of spam? Yahoo! Mail has the best spam protection around
      > http://mail.yahoo.com[/color]

      Comment

      • Bengt Richter

        #4
        Re: Random Number Generation?

        On Sun, 11 Dec 2005 09:46:33 -0800 (PST), Dimos <dimos_anastasi ou@yahoo.com> wrote:
        [color=blue]
        >Hello All,
        >
        >I need some help with random number generation. What I
        >need exactly is:
        >
        >To create a few thousand numbers, decimal and
        >integers, between 5 and 90,
        >and then to export them as a single column at a
        >spreadsheet.
        >
        >I am newbie, I was not able to create decimals with
        >the random modules of
        >Python 2.3.
        >[/color]
        Others have mentioned random.random and, better for your use case,
        random.uniform, but I'm not sure what you mean by "decimal and integers".

        Theoretically, the chances of getting an integer from a uniformly random
        sample from an interval of real numbers is practically zero, and even
        allowing for IEEE 754 double representation, the realtive population of
        integers vs non-integers is pretty low. So what do you mean by "integer"?
        And what by "decimals"?

        If you just want an artificial sprinkling of exact integer values to
        happen some percentage of the time, you could do something like
        [color=blue][color=green][color=darkred]
        >>> from random import uniform, random
        >>> def urnmix(nnum=20, lo=5, hi=90, percentint=25):[/color][/color][/color]
        ... percentint /= 100.
        ... for _ in xrange(nnum):
        ... u = uniform(5, 90)
        ... if random()<percen tint: u = round(u)
        ... yield u
        ...[color=blue][color=green][color=darkred]
        >>> for u in urnmix(12): print '%6.3f'%u,[/color][/color][/color]
        ...
        9.000 38.173 59.829 37.090 80.504 34.000 69.989 26.000 72.502 64.000 55.000 9.043[color=blue][color=green][color=darkred]
        >>> for u in urnmix(12): print '%6.3f'%u,[/color][/color][/color]
        ...
        10.000 67.687 70.323 66.672 17.150 68.447 84.406 6.997 82.444 8.001 82.946 34.849[color=blue][color=green][color=darkred]
        >>> for u in urnmix(12): print '%6.3f'%u,[/color][/color][/color]
        ...
        70.000 64.000 36.537 75.270 67.000 70.873 28.446 18.483 75.086 41.703 82.885 30.558[color=blue][color=green][color=darkred]
        >>> for u in urnmix(12): print '%6.3f'%u,[/color][/color][/color]
        ...
        75.000 78.313 76.873 48.364 12.000 40.000 36.962 27.704 8.814 44.078 61.000 35.654[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        Hm, let's check the percentages
        [color=blue][color=green][color=darkred]
        >>> [u==int(u) for u in urnmix(12)][/color][/color][/color]
        [True, False, False, False, False, True, True, True, True, False, True, False][color=blue][color=green][color=darkred]
        >>> [u==int(u) for u in urnmix(12)].count(True)[/color][/color][/color]
        2[color=blue][color=green][color=darkred]
        >>> [u==int(u) for u in urnmix(12)].count(True)[/color][/color][/color]
        4[color=blue][color=green][color=darkred]
        >>> [u==int(u) for u in urnmix(10000)].count(True)[/color][/color][/color]
        2471[color=blue][color=green][color=darkred]
        >>> [u==int(u) for u in urnmix(10000)].count(True)[/color][/color][/color]
        2539

        Seems to work ...
        [color=blue][color=green][color=darkred]
        >>> [u==int(u) for u in urnmix(10000, 5, 90, 5)].count(True)[/color][/color][/color]
        497[color=blue][color=green][color=darkred]
        >>> [u==int(u) for u in urnmix(10000, 5, 90, 1)].count(True)[/color][/color][/color]
        111[color=blue][color=green][color=darkred]
        >>> [u==int(u) for u in urnmix(10000, 5, 90, 1)].count(True)[/color][/color][/color]
        87[color=blue][color=green][color=darkred]
        >>> [u==int(u) for u in urnmix(10000, 5, 90, 1)].count(True)[/color][/color][/color]
        113

        After all this playing, what was it you actually wanted? ;-)

        Regards,
        Bengt Richter

        Comment

        • Mike Meyer

          #5
          Re: Random Number Generation?

          bokr@oz.net (Bengt Richter) writes:[color=blue]
          > Theoretically, the chances of getting an integer from a uniformly
          > random sample from an interval of real numbers is practically zero,
          > and even allowing for IEEE 754 double representation,[/color]

          Well, if we're going to be picky, the chances of getting a number with
          an IEEE 754 representation from a uniformly random sample from an
          interval of real numbers is practically zero. Of course, this is true
          for *any* finite subset of the reals (such as the set of numbers that
          have names that can be pronounced in the average human lifespan), and
          probably an infinite number of infinite subsets as well.

          But I tend to pick irrationals when asked to "pick a number between 1
          and 10."
          [color=blue]
          > So what do you mean by "integer"?
          > And what by "decimals"?[/color]

          I think we should start by finding out what he means by "number",
          which is apparently a superset of both what he means by "integer" and
          "decimals".

          <mike
          --
          Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
          Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

          Comment

          Working...