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"? > 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()) Not as an optimisation (replacing randint() with randrange() is probably more effective in that department), but because it's closer to being self- explanatory. > maximum = max(freq.values()) > return (minimum, maximum, minimum / maximum) -- https://mail.python.org/mailman/listinfo/python-list