niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » python
random shuffles
Post new topic   Reply to topic Page 1 of 1 [5 Posts] View previous topic :: View next topic
Author Message
Boris Borcic
*nix forums addict


Joined: 18 Apr 2006
Posts: 52

PostPosted: Fri Jul 21, 2006 11:11 am    Post subject: random shuffles Reply with quote

does

x.sort(cmp = lambda x,y : cmp(random.random(),0.5))

pick a random shuffle of x with uniform distribution ?

Intuitively, assuming list.sort() does a minimal number of comparisons to
achieve the sort, I'd say the answer is yes. But I don't feel quite confortable
with the intuition... can anyone think of a more solid argumentation ?

- BB
--
"On naît tous les mètres du même monde"
Back to top
Peter Otten
*nix forums Guru


Joined: 20 Feb 2005
Posts: 464

PostPosted: Fri Jul 21, 2006 11:32 am    Post subject: Re: random shuffles Reply with quote

Boris Borcic wrote:

Quote:
does

x.sort(cmp = lambda x,y : cmp(random.random(),0.5))

pick a random shuffle of x with uniform distribution ?

Intuitively, assuming list.sort() does a minimal number of comparisons to
achieve the sort, I'd say the answer is yes. But I don't feel quite
confortable with the intuition... can anyone think of a more solid
argumentation ?

Anecdotal evidence suggests the answer is no:

Quote:
histo = {}
for i in xrange(1000):
.... t = tuple(sorted(range(3), lambda x, y: cmp(random.random(), 0.5)))

.... histo[t] = histo.get(t, 0) + 1
....
Quote:
sorted(histo.values())
[60, 62, 64, 122, 334, 358]


versus:

Quote:
histo = {}
for i in xrange(1000):
.... t = tuple(sorted(range(3), key=lambda x: random.random()))

.... histo[t] = histo.get(t, 0) + 1
....
Quote:
sorted(histo.values())
[147, 158, 160, 164, 183, 188]


Peter
Back to top
Dustan
*nix forums beginner


Joined: 21 Nov 2005
Posts: 36

PostPosted: Fri Jul 21, 2006 12:22 pm    Post subject: Re: random shuffles Reply with quote

Boris Borcic wrote:
Quote:
does

x.sort(cmp = lambda x,y : cmp(random.random(),0.5))

pick a random shuffle of x with uniform distribution ?

Intuitively, assuming list.sort() does a minimal number of comparisons to
achieve the sort, I'd say the answer is yes. But I don't feel quite confortable
with the intuition... can anyone think of a more solid argumentation ?

Why not use the supplied shuffle method?

random.shuffle(x)
Back to top
Iain King
*nix forums addict


Joined: 16 Sep 2005
Posts: 70

PostPosted: Fri Jul 21, 2006 12:34 pm    Post subject: Re: random shuffles Reply with quote

Dustan wrote:
Quote:
Boris Borcic wrote:
does

x.sort(cmp = lambda x,y : cmp(random.random(),0.5))

pick a random shuffle of x with uniform distribution ?

Intuitively, assuming list.sort() does a minimal number of comparisons to
achieve the sort, I'd say the answer is yes. But I don't feel quite confortable
with the intuition... can anyone think of a more solid argumentation ?

Why not use the supplied shuffle method?

random.shuffle(x)

or check out this thread:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/766f4dcc92ff6545?tvc=2&q=shuffle

Iain
Back to top
Tim Peters
*nix forums Guru Wannabe


Joined: 26 Feb 2005
Posts: 192

PostPosted: Fri Jul 21, 2006 1:18 pm    Post subject: Re: random shuffles Reply with quote

[ Boris Borcic]
Quote:
x.sort(cmp = lambda x,y : cmp(random.random(),0.5))

pick a random shuffle of x with uniform distribution ?

Say len(x) == N. With Python's current sort, the conjecture is true
if and only if N <= 2.

Quote:
Intuitively, assuming list.sort() does a minimal number of comparisons to
achieve the sort,

No idea what that could mean in a rigorous, algorithm-independent way.

Quote:
I'd say the answer is yes. But I don't feel quite confortable
with the intuition... can anyone think of a more solid argumentation ?

If a list is already sorted, or reverse-sorted, the minimum number of
comparisons needed to determine that is N-1, and Python's current sort
achieves that. It first looks for the longest ascending or descending
run. If comparison outcomes are random, it will decide "yes, already
sorted" with probability 1/2**(N-1), and likewise for reverse-sorted.
When N > 2, those are larger than the "correct" probabilities 1/(N!).
So., e.g., when N=3, the sort will leave the list alone 1/4th of the
time (and will reverse the list in-place another 1/4th). That makes
the identity and reversal permutations much more likely than "they
should be", and the bias is worse as N increases.

Of course random.shuffle(x) is the intended way to effect a
permutation uniformly at random.
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [5 Posts] View previous topic :: View next topic
The time now is Sat Nov 22, 2008 9:26 pm | All times are GMT
navigation Forum index » Programming » python
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts random timeout / delivery temporarily suspended unarcher Postfix 0 Fri Jun 13, 2008 12:33 pm
No new posts Random Number Excluding Specified Value in Array AP PHP 5 Mon Jul 17, 2006 7:49 pm
No new posts Random Table Lock mysql@top-consulting.net MySQL 0 Mon Jul 17, 2006 11:39 am
No new posts Newbie question - displaying trivia questions at random philbo30 C 2 Sun Jul 16, 2006 8:39 pm
No new posts Random Hangs, Linux AMD 64, 5.0.22 AB Binaries Matt Williams MySQL 2 Thu Jul 13, 2006 5:48 pm

Mortgage Calculator | Consolidate Student Loans | Adverse Credit Remortgage | Mobile Phones | Loans
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2607s ][ Queries: 16 (0.1734s) ][ GZIP on - Debug on ]