[Tutor] random number equations . . .

Glen Wheeler gew75 at hotmail.com
Thu Jun 3 19:18:29 EDT 2004


  Hi Danny,

  You are of course correct!  My purpose in showing the usage of creating
one's own random numbers from random.random() is for understanding and
flexibility.  I guess if I was to write a ``proper'' myRandRange(..)
function I'd probably add 1 to the range then truncate.  Actually, let's try
that:

>>> def myRandRange(n):
..  return int((random.random()*(n+1)))
>>> distribution([myRandRange(2) for i in range(1000)])
{0: 320, 1: 331, 2: 349}

  Seems to work ok.
  Bias is one reason why someone may *want* to use their own function for
this kind of purpose.  I know that in several of my applications, I wish to
generate random numbers based on a custom distribution, so I have this set
up in a function.
  Now before you go off and point me to the docs, I do know that the random
module has builtin distributions, and do know how to express distributions
as functions of other standard distributions.  But not every single
distribution can be expressed as such, and it is easier to debug when you
know exactly what is going on.
  I guess my ``teach the basics first'' philosophy shows through in a bad
way sometimes.

-- 
Glen

----- Original Message ----- 
From: "Danny Yoo" <dyoo at hkn.eecs.berkeley.edu>
To: "Glen Wheeler" <gew75 at hotmail.com>
Cc: <Dragonfirebane at aol.com>; <tutor at python.org>
Sent: Friday, June 04, 2004 8:39 AM
Subject: Re: [Tutor] random number equations . . .


>
> Hi Glen,
>
> But whenever we're dealing with probability, it's often a good idea to
> reuse what other folks have already done.  *grin*
>
>
> This kind of selection function already exists in random.randrange():
>
>     http://www.python.org/doc/lib/module-random.html#l2h-1147
>
>
> The problem with gimmeRandomInRange() is that it's biased.  It's easier to
> see what this means if we use a small range.  Let's see what happens when
> we use it for a range between 0 and 2, inclusive:
>
> ###
> >>> def distribution(numbers):
> ...     """Calculates a distribution of the numbers."""
> ...     counts = {}
> ...     for n in numbers:
> ...         counts[n] = counts.get(n, 0) + 1
> ...     return counts
> ...
> >>> distribution([gimmeRandomInRange(2) for i in range(1000)])
> {0: 238, 1: 523, 2: 239}
> ###
>
>
> There's a big hump near one!
>
>
> Why is that?  Why are the numbers biased biased around 1?  If we draw
> things out:
>
>                 A            B           C
>             |--------(---------------)-------|
>             0       0.5      1      1.5      2
>
>
> our number line splits into three regions A, B, and C.  The behavior of
> the round()ing causes region B to be larger than the other two.
>
>
> But random.randrange() doesn't suffer this defect:
>
> ###
> >>> distribution([random.randrange(0, 3) for i in range(1000)])
> {0: 343, 1: 344, 2: 313}
> ###
>
>
> The 'random' module has many helper functions that we should use.
> They're there because it's all too easy not to take into consideration
> some of the subtle problems with random number generation.
>
>
>
> Hope this helps!
>
>



More information about the Tutor mailing list