473,698 Members | 2,450 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

random shuffles

does

x.sort(cmp = lambda x,y : cmp(random.rand om(),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"
Jul 21 '06 #1
19 3077
Boris Borcic wrote:
does

x.sort(cmp = lambda x,y : cmp(random.rand om(),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:
>>histo = {}
for i in xrange(1000):
.... t = tuple(sorted(ra nge(3), lambda x, y: cmp(random.rand om(), 0.5)))
.... histo[t] = histo.get(t, 0) + 1
....
>>sorted(histo. values())
[60, 62, 64, 122, 334, 358]

versus:
>>histo = {}
for i in xrange(1000):
.... t = tuple(sorted(ra nge(3), key=lambda x: random.random() ))
.... histo[t] = histo.get(t, 0) + 1
....
>>sorted(histo. values())
[147, 158, 160, 164, 183, 188]

Peter
Jul 21 '06 #2

Boris Borcic wrote:
does

x.sort(cmp = lambda x,y : cmp(random.rand om(),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)

Jul 21 '06 #3

Dustan wrote:
Boris Borcic wrote:
does

x.sort(cmp = lambda x,y : cmp(random.rand om(),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....vc=2&q=shuffle

Iain

Jul 21 '06 #4
[ Boris Borcic]
x.sort(cmp = lambda x,y : cmp(random.rand om(),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.
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.
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.
Jul 21 '06 #5
Boris Borcic <bb*****@gmail. comwrites:
x.sort(cmp = lambda x,y : cmp(random.rand om(),0.5))
pick a random shuffle of x with uniform distribution ?
You really can't assume anything like that. Sorting assumes an order
relation on the items being sorted, which means if a < b and b < c,
then a < c. If your comparison operation doesn't preserve that
property then the sort algorithm could do anything, including looping
forever or crashing.
Jul 21 '06 #6
Paul Rubin wrote:
Boris Borcic <bb*****@gmail. comwrites:
>x.sort(cmp = lambda x,y : cmp(random.rand om(),0.5))
pick a random shuffle of x with uniform distribution ?
You really can't assume anything like that. Sorting assumes an order
relation on the items being sorted, which means if a < b and b < c,
then a < c. If your comparison operation doesn't preserve that
property then the sort algorithm could do anything, including looping
forever or crashing.
Not if it does the minimum number of comparisons to achieve the sort, in which
case it won't ever call cmp() if the result is pre-determined by the order
relation and previous comparisons, so that it will never get from this
comparison function a system of answers that's not consistent with an order
relation. That's obvious at least in the case where random.random() ==0.5 never
occurs (and at first sight even the latter case shouldn't change it).

Best, BB
--
"On naît tous les mètres du même monde"

Jul 21 '06 #7
Thanks for these details. BB

Tim Peters wrote:
[ Boris Borcic]
>x.sort(cmp = lambda x,y : cmp(random.rand om(),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.
>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.
>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.
Jul 21 '06 #8
Boris Borcic wrote:
Paul Rubin wrote:
>Boris Borcic <bb*****@gmail. comwrites:
>>x.sort(cmp = lambda x,y : cmp(random.rand om(),0.5))
pick a random shuffle of x with uniform distribution ?

You really can't assume anything like that. Sorting assumes an order
relation on the items being sorted, which means if a < b and b < c,
then a < c. If your comparison operation doesn't preserve that
property then the sort algorithm could do anything, including looping
forever or crashing.
Not if it does the minimum number of comparisons to achieve the sort, in
which case it won't ever call cmp() if the result is pre-determined by
the order relation and previous comparisons, so that it will never get
read "by /the assumption of an order relation/ and previous comparisons"
from this comparison function a system of answers that's not consistent
with an order relation. That's obvious at least in the case where
random.random() == 0.5 never occurs (and at first sight even the latter
case shouldn't change it).
- this - the idea that /if/ the algorithm was optimal it would only sample the
random comparison function up to a system of answers consistent with an order
relation - is actually what prompted my question,
iow "is it good for anything ?"

Best, BB
--
"On naît tous les mètres du même monde"
Jul 21 '06 #9
Boris Borcic wrote:
Paul Rubin wrote:
>Boris Borcic <bb*****@gmail. comwrites:
>>x.sort(cmp = lambda x,y : cmp(random.rand om(),0.5))
pick a random shuffle of x with uniform distribution ?

You really can't assume anything like that. Sorting assumes an order
relation on the items being sorted, which means if a < b and b < c,
then a < c. If your comparison operation doesn't preserve that
property then the sort algorithm could do anything, including looping
forever or crashing.
Not if it does the minimum number of comparisons to achieve the sort, in
which case it won't ever call cmp() if the result is pre-determined by
the order relation and previous comparisons, so that it will never get
from this comparison function a system of answers that's not consistent
with an order relation. That's obvious at least in the case where
random.random() == 0.5 never occurs (and at first sight even the latter
case shouldn't change it).
To be more convincing... assume the algorithm is optimal and calls
cmp(x,y). Either the previous calls (+ assuming order relation) give no
conclusion as to the possible result of the call, in which case the result can't
contradict an order relation; or the previous calls (+assumption) provideonly
partial information; iow the algorithm knows for example x<=y and needsto
determine which of x<y or x==y is the case; in which situation the comparison
function could break the assumption of an order relation by answering eg "x>y".

But is it possible for a sort algorithm assuming an order relation to attain eg
x<=y but neither x<y nor x==y using calls to the comparison function involving
either x or y and some other variables ? No, since the axiom of order applies to
data that never has the form of weak inequalities and thus will never infer an
inequality that's not strong.

Ok, this isn't well written, but I have to run.

Cheers, BB
--
"On naît tous les mètres du même monde".
Jul 21 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
3144
by: Paul C-T | last post by:
Hi, Is there a way to repeat a set of code until a certain if statement is satisfied. If it is then to exit the loop and if not repeat the code? Say I want to write a card game application in PHP and I want to chose a card from the deck $card1 = rand(1,52); gets me my first card. I need to record which card has been dealt so I have a variable $dealt which starts out as a string of 52 zeros. I update the position of $card1 in the
23
4179
by: Thomas Mlynarczyk | last post by:
I remember there is a programming language where you can initialize the random number generator, so that it can - if you want - give you the exactly same sequence of random numbers every time you initialize it with the same parameter. Can this be done with JavaScript? I couldn't find anything in the documentation. Basically, what I want to achieve is to obtain always the same sequence of random numbers for the same given initialization...
1
4718
by: steflhermitte | last post by:
Dear cpp-ians, I want to apply a stratified sampling on an image. Following simplified example will explain my problem. The original image I with nrows and ncols is now a vector V of length (nrow x ncol) and every element of the vector contians its pixel value. vector float V ;
15
3320
by: alanbe | last post by:
Greetings I am making a flashcard type application to help me in my TCP/IP protocols test. My instructor will test us periodically on how a device or networking function relates to the OSI layer. EG. bits-layer 1. Any way, I want the quiz to reorder the problems each time I take it. Here is part of the code i did so far for 62 components in the quiz.
9
28834
by: gl | last post by:
How do I take an array or arraylist, and sort it randomly? Like suppose the items in it are (1,2,3,4,5) and I want to get it to be in a random order (2,3,1,4,5). How do you do that? I think it's a call to the array or array lists sort method, but i'm not exactly sure how you do it.
104
5149
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a better way than to fill an array of range 0... RAND_MAX with pre-computed primes and using the output of rand() to index into it to extract a random prime.
5
2077
by: jar13861 | last post by:
I'm confused on how to write a random array that will only generate 9 different numbers from 1-9. Here is what I have, but its only writing one number.... holder = new Array ( 9 ); var flag = true; var rannum = Math.floor( 1 + Math.random() * 9 ); for (var j = 0; j < 9; j++) {
19
4216
by: Erich Pul | last post by:
hi! i got a structure, which should be filled with random integer values (it is in fact a generator for numbers like in a lotto), but these values must not be recurring, so no double occurrences are desired. my code: <code> Tipp* ziehung() // Tipp* is the function
12
3220
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to split a number in several pieces in a random way without success. for example if the number is 123 456 : i would like to have some random strings like theese : (12 * 10 000) + (345 * 10) + (6*1) or (123*1 000)+(4*100)+(5*10)+(6*1) etc...
0
9160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8862
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2331
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.