Bugs item #1551113, was opened at 2006-09-02 18:27
Message generated for change (Comment added) made by gbrandl
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1551113&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Alan (aisaac0)
>Assigned to: Raymond Hettinger (rhettinger)
Summary: random.choice(setinstance) fails

Initial Comment:
It seems perverse that random.choice cannot pick from 
a set.
(Maybe it is also perverse that it cannot pick a key 
from a dict.)
The current definition is

def choice(self, seq):
    """Choose a random element from a non-empty 
sequence."""
    return seq[int(self.random() * len(seq))]  # 
raises IndexError if seq is empty

How about something along the lines of:

def choice(self, seq):
    """Choose a random element from a non-empty 
sequence."""
    idx = int(self.random() * len(seq))
    try:
        result = seq[idx]  # raises IndexError if seq 
is empty
    except TypeError:
        result = list(seq)[idx]
    return result
 


----------------------------------------------------------------------

>Comment By: Georg Brandl (gbrandl)
Date: 2006-09-02 18:33

Message:
Logged In: YES 
user_id=849994

To Raymond: Wasn't there a discussion about random.choice()
some time ago?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1551113&group_id=5470
_______________________________________________
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to