Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'element': 0.07; 'badly': 0.09; 'cc:addr:python-list': 0.11; 'random': 0.14; '...,': 0.16; 'bisect': 0.16; 'choice,': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'integer.': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'received:211.29': 0.16; 'received:211.29.132': 0.16; 'received:cskk.homeip.net': 0.16; 'received:homeip.net': 0.16; 'received:optusnet.com.au': 0.16; 'received:syd.optusnet.com.au': 0.16; 'simpson': 0.16; 'storing': 0.16; 'tuple': 0.16; 'weight,': 0.16; 'wrote:': 0.18; 'slightly': 0.19; 'cc:addr:python.org': 0.22; 'header:User-Agent:1': 0.23; '(by': 0.24; 'choices': 0.24; 'integer': 0.24; 'module,': 0.24; 'cheers,': 0.24; '(or': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'header:In-Reply- To:1': 0.27; 'this.': 0.32; 'beginning': 0.33; 'ordered': 0.36; 'received:com.au': 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'list': 0.37; 'received:211': 0.38; 'even': 0.60; 'range': 0.61; 'first': 0.61; 'content-disposition:inline': 0.62; 'high': 0.63; 'soon': 0.63; 'sum': 0.64; 'containing': 0.69 Date: Mon, 9 Sep 2013 10:49:13 +1000 From: Cameron Simpson To: Dennis Lee Bieber Subject: Re: Weighted choices MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) References: X-Optus-CM-Score: 0 X-Optus-CM-Analysis: v=2.1 cv=DstvpgP+ c=1 sm=1 tr=0 a=YuQlxtEQCowy2cfE5kc7TA==:117 a=YuQlxtEQCowy2cfE5kc7TA==:17 a=ZtCCktOnAAAA:8 a=PO7r1zJSAAAA:8 a=LcaDllckn3IA:10 a=kj9zAlcOel0A:10 a=vrnE16BAAAAA:8 a=U-3a9JORWjgA:10 a=QrVWhbZvAAAA:8 a=ZTcsHeSLr7ag9N-UYmsA:9 a=CjuIK1q_8ugA:10 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 23 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1378687769 news.xs4all.nl 15984 [2001:888:2000:d::a6]:48941 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:53851 On 08Sep2013 20:21, Dennis Lee Bieber wrote: | However, I would not use a dictionary for this. An ordered list should | work better... for small samples a list containing repeats (by weight) of | each choice, and then use a random integer whose range is 0..len(list)-1 | would suffice. | | choices = ["apple", "apple", "apple", ..., "kiwi", "kiwi" ] Scales badly as soon as the weights get even slightly big (or high res). | For longer lists, storing a tuple with the accumulating weight, and | scanning from the beginning | | choices = [(10, "Apple"), (10+20, "Pear"), (10+20+15, "Banana")... ] | | generate random integer in the range of the sum of the weights, and accept | the last tuple whose first element is less than the integer. Search it with the bisect module, not linearly! Cheers, -- Cameron Simpson