Biased random?
Robert Kern
robert.kern at gmail.com
Fri Aug 31 14:40:59 EDT 2007
Jeffrey Barish wrote:
> 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.
"The math" says nothing of the kind about the method that was stated.
> 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.
That's a very different "it". The difference is not just implementation.
If you change "round" to "truncate", that method should work, though.
--
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