Opened 11 years ago

Last modified 7 years ago

#10634 needs_work enhancement

Weighted Choice

Reported by: eviatarbach Owned by: jason
Priority: minor Milestone: sage-6.4
Component: misc Keywords:
Cc: Merged in:
Authors: Eviatar Bach Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Status badges

Description

This adds weighted choice to the choice function in prandom.py.

Attachments (1)

15208.patch (2.2 KB) - added by eviatarbach 11 years ago.

Download all attachments as: .zip

Change History (8)

Changed 11 years ago by eviatarbach

comment:1 Changed 11 years ago by eviatarbach

  • Status changed from new to needs_review

comment:2 Changed 11 years ago 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

comment:3 Changed 11 years ago by ncohen

  • Status changed from needs_review to needs_work

Until the previous comments are addressed/answered... :-)

Oh, and it would be nice to keep the lines to less than 80 characters !

Nathann

comment:4 Changed 8 years ago by jdemeyer

  • Milestone changed from sage-5.11 to sage-5.12

comment:5 Changed 8 years ago by vbraun_spam

  • Milestone changed from sage-6.1 to sage-6.2

comment:6 Changed 8 years ago by vbraun_spam

  • Milestone changed from sage-6.2 to sage-6.3

comment:7 Changed 7 years ago by vbraun_spam

  • Milestone changed from sage-6.3 to sage-6.4
Note: See TracTickets for help on using tickets.