Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Biased random?


  • From: jeff_barish at earthlink.net (Jeffrey Barish)
  • Subject: Biased random?
  • Date: Fri, 31 Aug 2007 09:18:34 -0600

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