Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.python Subject: Re: Testing random Date: Sun, 07 Jun 2015 12:40:41 +0200 Organization: PointedEars Software (PES) Lines: 46 Message-ID: <1451048.pW9z17ilMA@PointedEars.de> References: <87oaksowwg.fsf@Equus.decebal.nl> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1433673802 6916 eJwNyMkRwDAIBLCWMGcoJyzQfwn26CcTP45QN1db20GVxEqScMeLyN+bqwWgaSTrgy806UzZBS7VEWM= (7 Jun 2015 10:43:22 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Sun, 7 Jun 2015 10:43:22 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwFwYEBwDAIArCbrEB9p+L8/4QlTIV8IQpcbvmYQ/ct3OjyrRecjYOe/MadFCkRWFe+l4xGGSZa+wNZQBWX Cancel-Lock: sha1:2z5FL+cBN2LBZYEN2bUgg/X3LtU= X-NNTP-Posting-Host: eJwFwYkBwCAIA8CVtJIg4xie/UfoHQ43042gYTC59HzPrYZTho9szeVroSIFBOJUSzmzOn8xHBJh Xref: csiph.com comp.lang.python:92226 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 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.