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


Groups > comp.lang.python > #53850

Re: Weighted choices

Path csiph.com!usenet.pasdenom.info!goblin1!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.014
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'element': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'question.': 0.14; 'random': 0.14; '...,': 0.16; 'choice,': 0.16; 'fruit': 0.16; 'integer.': 0.16; 'message-id:@4ax.com': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'set,': 0.16; 'storing': 0.16; 'tuple': 0.16; 'weight,': 0.16; 'example': 0.22; '(by': 0.24; 'choices': 0.24; 'integer': 0.24; 'url:home': 0.24; 'regardless': 0.24; 'header:X-Complaints-To:1': 0.27; 'point': 0.28; 'appear': 0.29; 'sets': 0.30; 'sep': 0.31; 'this.': 0.32; 'beginning': 0.33; 'convert': 0.35; 'but': 0.35; '+0200,': 0.36; 'acceptable': 0.36; 'ordered': 0.36; 'charset:us- ascii': 0.36; 'should': 0.36; 'list': 0.37; 'received:76': 0.38; 'jason': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'above,': 0.60; 'range': 0.61; 'first': 0.61; 'pick': 0.64; 'sum': 0.64; 'total': 0.65; 'chance': 0.65; 'talking': 0.65; 'containing': 0.69; 'dict()': 0.84; 'pardon': 0.84; '2013': 0.98
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: Weighted choices
Date Sun, 08 Sep 2013 20:21:32 -0400
Organization IISS Elusive Unicorn
References <CANy1k1gHFFgd82P5VZJEEZvWe_kHJvi2r1uqVh3Sr=nFZyazuQ@mail.gmail.com> <522CB887.9090000@rece.vub.ac.be>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host adsl-76-249-24-8.dsl.klmzmi.sbcglobal.net
X-Newsreader Forte Agent 6.00/32.1186
X-No-Archive YES
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.160.1378686098.5461.python-list@python.org> (permalink)
Lines 41
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1378686098 news.xs4all.nl 15868 [2001:888:2000:d::a6]:45771
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:53850

Show key headers only | View raw


On Sun, 08 Sep 2013 19:48:55 +0200, Antoon Pardon
<antoon.pardon@rece.vub.ac.be> declaimed the following:

>Op 08-09-13 04:12, Jason Friedman schreef:
>> choices = dict()
>> choices["apple"] = 10
>> choices["pear"] = 20
>> choices["banana"] = 15
>> choices["orange"] = 25
>> choices["kiwi"] = 30
>>
>> I want to pick sets of fruit, three in a set, where the chance of
>> selecting a given fruit is proportional to its weight.  In the example
>> above, pears should appear twice as often as apples and kiwis should
>> appear twice as often as bananas.
>
>Just a small question. Is a set of three bananas an acceptable outcome?

	If we are talking probabilities, regardless of what the weighting is,
it should be probable (if unlikely) to get three-of-a-kind.

	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" ]

	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. {Or convert
the weights to floating point (by dividing each by the total sum), same
concept but random number in range 0.0..1.0}
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

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


Thread

Re: Weighted choices Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-09-08 20:21 -0400

csiph-web