Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #53851

Re: Weighted choices

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 <cameron@cskk.homeip.net>
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 <cs@zip.com.au>
To Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: Weighted choices
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Disposition inline
In-Reply-To <kl4q29ta0kn2tnfoju13dg8nni4vjvco80@4ax.com>
User-Agent Mutt/1.5.21 (2010-09-15)
References <kl4q29ta0kn2tnfoju13dg8nni4vjvco80@4ax.com>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.161.1378687769.5461.python-list@python.org> (permalink)
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

Show key headers only | View raw


On 08Sep2013 20:21, Dennis Lee Bieber <wlfraed@ix.netcom.com> 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 <cs@zip.com.au>

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Weighted choices Cameron Simpson <cs@zip.com.au> - 2013-09-09 10:49 +1000

csiph-web