Testing random

Steven D'Aprano steve at pearwood.info
Sun Jun 7 21:35:07 EDT 2015


On Sun, 7 Jun 2015 11:35 pm, Peter Otten wrote:

> Steven D'Aprano wrote:
> 
> 
>>> I wrote a very simple function to test random:
>>>     def test_random(length, multiplier = 10000):
>>>         number_list = length * [0]
>>>         for i in range(length * multiplier):
>>>             number_list[random.randint(0, length - 1)] += 1
>>>         minimum = min(number_list)
>>>         maximum = max(number_list)
>>>         return (minimum, maximum, minimum / maximum)
>> 
>> Putting aside the timing aspects, your frequency calculations are not
>> done in a very Pythonic manner.
> 
> I would agree if the frequency table were sparse, i. e. many indices with
> 
> number_list[index] == 0
> 
> but that's not the case with on average 10000 hits per index.
> 
>> A better way might be:
> 
> I'm too lazy to measure, but this will likely be a tad slower. Did you
> mean to convey this by "Putting aside the timing aspects"?

No, I meant that using a Counter to count stuff is more Pythonic than using
a list.



>> from collections import Counter
>> from random import randint
>> 
>> def test_random(length, multiplier = 10000):
>>     freq = Counter(
>>         randint(0, length - 1) for i in range(length * multiplier)
>>         )
>>     minimum = min(freq[i] for i in range(length))
> 
> How about
> 
>       if len(freq) < length:
>           minimum = 0
>       else:
>           minimum = min(freq.values())

That's quite nice, and it's extensible to the case where I return the
counter:

if len(freq) != length:
    # At least one element never showed up at all.
    freq.subtract(dict.fromkeys(range(length), 0)


 
> Not as an optimisation (replacing randint() with randrange() is probably
> more effective in that department), but because it's closer to being self-
> explanatory.

I thought about replacing randint with randrange, but they are different
functions with different implementations, and could exhibit different
biases. So one might wish to test them separately for statistical
properties.



-- 
Steven




More information about the Python-list mailing list