Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; '[0]': 0.07; 'indices': 0.07; 'collections': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.14; 'convey': 0.16; 'hits': 0.16; 'index.': 0.16; 'length:': 0.16; 'pythonic': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:random': 0.16; 'wrote:': 0.16; 'wrote': 0.23; 'import': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; "i'm": 0.29; 'be:': 0.29; 'closer': 0.29; 'random': 0.29; 'function': 0.30; 'putting': 0.31; 'table': 0.32; 'probably': 0.32; 'aside': 0.32; "d'aprano": 0.33; 'steven': 0.33; 'minimum': 0.35; 'to:addr:python-list': 0.35; 'done': 0.35; 'but': 0.36; 'being': 0.36; 'too': 0.36; 'agree': 0.37; 'subject:: ': 0.37; 'received:org': 0.38; 'mean': 0.38; 'test': 0.39; 'to:addr:python.org': 0.39; 'received:de': 0.40; 'your': 0.60; 'maximum': 0.61; 'per': 0.61; 'simple': 0.61; 'effective': 0.62; 'more': 0.62; 'skip:n 10': 0.63; '10000': 0.66; 'frequency': 0.66; 'calculations': 0.84; 'measure,': 0.84; 'optimisation': 0.84; 'subject:Testing': 0.84; 'average': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Testing random Date: Sun, 07 Jun 2015 15:35:55 +0200 Organization: None References: <87oaksowwg.fsf@Equus.decebal.nl> <55743ea4$0$13004$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd87cc.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1433684166 news.xs4all.nl 2959 [2001:888:2000:d::a6]:49794 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92252 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)