Biased random?

Jeffrey Barish jeff_barish at earthlink.net
Fri Aug 31 11:18:34 EDT 2007


Robert Kern wrote:

> Ivan Voras wrote:
>> Jeffrey Barish wrote:
>> 
>>> If you take the difference between two uniformly distributed random
>>> variables, the probability density function forms an isosceles triangle
>>> centered at 0.  Take the absolute value of that variable and the pdf is
>>> a
>>> straight line with maximum value at 0 tapering to 0 at max.  Thus,
>>>
>>> z = abs(randint(0, max) - randint(0, max))
>>>
>>> ought to do the trick.
>> 
>> It's elegant :)
>> 
>> I've noticed something interesting in my test: the value 0 appears less
>> often than other values (which behave as they should).
> 
> The distribution of the difference (before the abs()) looks like this
> (max=4):
> 
>     #
>    ###
>   #####
>  #######
>  ---0+++
>  321 123
> 
> Taking the absolute value doubles up the non-zero masses, but there's no
> "negative 0" to add to the 0s stack.
> 
>   #
>   #
>  ###
>  ###
>  ####
>  ####
>  0123
> 
> The method does not work because of that.
> 
The math says that it works, so we must not be implementing it correctly.  I
suspect that our mistake is quantizing the random variable first and then
taking the difference and absolute value.  What result do you get when you
quantize once?  That is, obtain two random values (floats) with uniform
pdf.  Take the difference.  Abs. Round to int.  This way, everything in the
band from (-0.5, +0.5) goes to 0, and that's the highest part of the
triangle.
-- 
Jeffrey Barish




More information about the Python-list mailing list