Random sort demonstration

Michael Mays michael_livecode at nayyan.com
Thu May 23 21:47:39 EDT 2013


Note that the variable lineAssignments in the quoted code below is always empty when it is displayed in the field and is empty each time the function randomLog is entered. You can make lineAssignment global to both handlers and it will work.

For a uniformly randomly shuffling function all outcomes should be equal. In other words all the possible outcomes:
a,b,c
a,c,b
b,a,c
c,b,a
b,c,a
c,a,b 
should all have equal chances of occurring. 

We can test our functions my letting them pick ten thousands item lists and then see if the occurrence of each list is equal. This is easy to test. Change the number of trail to 10000 and add these lines to the mouseUp handler
      put myVar into line cnt of fld 1
--new stuff start
      if myVar is "a,b,c" then add 1 to item 1 of my_sum
      if myVar is "a,c,b" then add 1 to item 2 of my_sum
      if myVar is "b,a,c" then add 1 to item 3 of my_sum
      if myVar is "c,a,b" then add 1 to item 4 of my_sum
      if myVar is "b,c,a" then add 1 to item 5 of my_sum
      if myVar is "c,b,a" then add 1 to item 6 of my_sum
--new stuff end

and  these lines
  end repeat
--new stuff start
   put return & my_sum into line cnt + 1 of fld 1
   put return&sum(my_sum) after fld 1 
end mouseUp
--new stuff end

This shows a really skewed non-uniform outcome for the sort by.
3783,1438,1467,1474,1459,379
10000
Whereas my shuffling is pretty uniform.
1671,1673,1728,1657,1612,1659 
10000

What you want to be able to make is a random assortment of
1,2,3
1,3,2
2,1,3
3,1,2
3,2,1
2,3,1
to sort your items against. But you aren't. You are generating potentially 27 different sets:
 1,1,1 ; 1,1,2 ; 1,1,3 ; 1,2,2 ; 1,2,3 ; 1,3,3 ; 1,3,2 ; 1,3,1 ; 1,2,1
{a,b,c ; a,b,c ; a,b,c ;  a,b,c ; a,b,c ; a,b,c ;  a,c,b ; a,c,b ; a,c,b}
 2,2,2 ; 2,2,1 ; 2,2,3 ; 2,1,1 ; 2,1,3 ; 2,3,3 ; 2,3,1 ; 2,3,2 ; 2,1,2
{a,b,c ; c,a,b ; a,b,c ; b,c,a ; b,a,c ; a,b,c ; c,a,b ; a,c,b ; b,a,c}
 3,3,3 ; 3,3,1 ; 3,3,1 ; 3,2,2 ; 3,2,1 ; 3,1,1 ; 3,1,2 ; 3,1,3;  3,2,3
{a,b,c ; c,a,b ; c,a,b ; b,c,a ; c,b,a ; b,c,a ; b,c,a ; b,a,c ; b,c,a}
which counts out as
a,b,c  10  [3700]
a,c,b  4    [1480]
b,a,c  3    [1100]
c,b,a  1       [370]
b,c,a  5    [1850]
c,a,b  4    [1480]
which looks similar to your results but is really different no less not a uniformly random distribution.

I really don't understand why when the random function is in line with the sort command the same distribution is always returned but when the random function is called in a function in line with the sort command it is different.

Michael


On May 23, 2013, at 2:08 PM, Dar Scott <dsc at swcp.com> wrote:

> This script demonstrates what is going on with random sorts of short lists using this:
> 
>      sort items of myVar by random( the number of items of myVar)
> 
> This is a button script on a stack with a wide field with a scrollbar.
<clipped due to 15K limit>




More information about the use-livecode mailing list