Biased random?

Robert Kern robert.kern at gmail.com
Thu Aug 30 16:42:15 EDT 2007


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.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco




More information about the Python-list mailing list