Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: random permutations

by gryng (Hermit)
on Mar 11, 2001 at 22:27 UTC ( [id://63638]=note: print w/replies, xml ) Need Help??


in reply to random permutations

I was about to comment that this doesn't work, that it should crash perl. But it seems that they have changed sort from under my eyes. Instead of saying sort will seg fault when given inconsistant results, it now just says sort's behavior is "not well-defined". Still sounds fishy, but testing the snippet out seems to suggest that it still works.

Here is a slightly more sparse version:
 my @list = sort {-1+int rand 3} (0..99);

Ciao,
Gryn

Replies are listed 'Best First'.
Re: Re: random permutations
by mave (Sexton) on Mar 11, 2001 at 22:55 UTC
    Thank you for your comments.

    Yes, the camel book states that some implementations of qsort do crash when fed inconsistent comparision results; that is a valid point.

    Also, your version feels more perl-like. But I actually choose the constants 100 and 50 in 'int(rand(100)-50)' to have only 2% chance on an equality result (i.e. 0); my intuition tells me that thus the result will be more random (this will depend on the implementation of qsort; but qsort will typically take the same decission when faced with equality between the elements).

    Greetz -- Mave

      Hi mave,

      The text-book way of doing qsort 'properly' (already I'm on flame-war ground :) ), is use the median-three variant (median-three vairant means pick the first, middle and last item and choose the median of these for the partition point)with three speed enhancements: first use the two remaining items from the median-three step as sentinels (instead of having a second conditional in the innerloop), next at n=20-60ish (hardware dependent constant) switch to insertion sort, and lastly when deciding wether to swap or keep in place an equal item, choose to swap it.

      Of course, all this is meaningless babble. The best thing to do is go in and test it. Hmm, unfortunately my boss got back from out of town work and is on the rampage this morning handing out more work for us all :) . I suggest a randomness test on output (chi-squared) and/or running sample data and counting the number of swaps. I also suggest using this when testing:
      my @list = map $_->[1], sort {$a->[0]<=>$b->[0]} map [rand,$_], (0..99);

      Which should be much faster on larger sets, and you can change the rand part to int rand $n, which can have $n be the (inverse of the) chance that two items will be equal.

      Good Luck,
      Gryn

        Last time I recall this being tested here (I leave the search to you), it was obvious from fairly casual inspection that the list was not well randomized at all.

                - tye (but my friends call me "Tye")
      If you want it to be either -1 or 1, but never 0, you could use
      sort { int(rand 2)*2-1 } @list

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://63638]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (1)
As of 2024-04-24 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found