Testing random
Thomas 'PointedEars' Lahn
PointedEars at web.de
Sun Jun 7 06:40:41 EDT 2015
Cecil Westerhof 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)
As there is no guarantee that every number will occur randomly, using a
dictionary at first should be more efficient than a list:
def test_random (length, multiplier=10000):
number_list = {}
for i in range(length * multiplier):
r = random.randint(0, length - 1)
number_list[r] = number_list[r] + 1 if r in number_list else 1
values = number_list.values()
minimum = min(values)
maximum = max(values)
return (minimum, maximum, minimum / maximum)
[Š¢uple literals are going to be introduced with C# 7 ;-)]
Since Python uses indentation as syntax element, it is better not to indent
Python code by default when posting in order to make it easier testable. If
necessary, code sections can be delimited in an unambiguous way that is
syntactically correct by lines like
#------------------------------------------------------------------------
The line length then should not exceed 72 to 78 characters, for quoting.
Although the <HT> character can be used for indentation, 4 spaces appear to
be a sensible, device-independent default (8 is too much for more complex
snippets that need to fit within the conventional 80-characters-per-line
limit).
--
PointedEars
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
More information about the Python-list
mailing list