#10634: Weighted Choice
----------------------------+-----------------------------------------------
Reporter: eviatarbach | Owner: jason
Type: enhancement | Status: needs_review
Priority: minor | Milestone: sage-4.6.2
Component: misc | Keywords:
Author: Eviatar Bach | Upstream: N/A
Reviewer: | Merged:
Work_issues: |
----------------------------+-----------------------------------------------
Comment(by dsm):
A few points:
(1) There are a few typos in the doc ("choose choose", and "if given a
list" reads better than "if inputted a list").
(2) The use of factor=1/max(seq.values()) means that the code can run
forever if the maximum weight is a Python integer > 1, because then factor
becomes 0.
{{{
sage: choice({1:int(2)})
[forever..]
}}}
(3) I don't know if it matters, but the while True approach can be quite
inefficient for large arrays, because the odds of succeeding on any given
try can be pretty low:
{{{
sage: d = dict((i,1) for i in range(10**4))
sage: time z = choice(d)
CPU times: user 0.14 s, sys: 0.00 s, total: 0.14 s
Wall time: 0.15 s
sage: time z = choice(range(10**4))
CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
Wall time: 0.00 s
}}}
--
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/10634#comment:2>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sage-trac?hl=en.