randomly order a list

Dar Scott dsc at swcp.com
Thu May 23 11:16:50 CDT 2013


Yes, sorting involves picking.  But this is assigning a sorting value to each item or line.  Each item or line is assigned a value independently; the history of assigning previous values does not matter.  

Only then is the sorting performed (virtually).  

It is possible for some lines to be assigned the same value.  They do not change sort order.  With large numbers, this is trivial.  With small arguments to random(), this becomes important.  

We can think of it like this.  Suppose we are sorting lines.  We add a random number as an item to the front of each line.  Some lines will get the same number.  Then we sort 'by item 1 of each', that is, by the random number.  Those lines with the same number will not change relative positions.  Then we remove the random number in front.  We have our scrambled list.  If that random number is chosen from {1,2,3} and the list is 3 lines, then there is a 14/27 chance that the first line will stay the first line instead of 1/3 as we want.  The 'by random(3)' works the same.  It assigns a value to each line.  The 'by random(99999)' has less of a chance of same values.

This is confirmed by empirical tests.

Dar


On May 23, 2013, at 5:36 AM, Michael Mays wrote:

> No it is 3 factorial. Jacques and Craig and I are right. 
> 
> This is picking without replacement, not picking with replacement. The first answer position has three choices. Once you pick it the second answer position has 2 and the final position 1 option left. To get 27 options means you have answer sheets where the first answer is option 3, you return option 3 to the list and the second answer is option 3 and you return option 3 to the list and the third question is option three and other such. Sort by defaults to an ordering which makes sense in these cases making it look like you have a right answer ordering. 
> 
> Michael
> 
> On May 23, 2013, at 12:20 PM, Dar Scott <dsc at swcp.com> wrote:
> 
>> Easy mistake to make, Jacques, but it is not 3!.
>> 
>> Random() emulates independent random numbers.  It cannot avoid numbers returned before in the sort.  Each line will get a randomly assigned number independent of the other lines.  
>> 
>> The number of ways 3 lines can be ordered does not apply.
>> 
>> Each line will be assigned a number by random().  So it is possible that the first line will be assigned a 2 and the other lines assigned 2, also.  The probability of that is one out of 3^3.  (Not three factorial)
>> 
>> You can create a function to log the assignments and use that as the comparison metric function.  You can see the assignments, and different lines can have the same value.  
>> 
>> Each sort will get a random pattern of 1, 2 and 3.  There are 27 of them.
>> 
>> Fourteen of them will cause a sort with the first line coming first again.  That is 52%, not 33%.  
>> 
>> You can write code to sort the same starting string a thousand times and the same first line will show up 52% of the time, not 33%.  
>> 
>> Dar
>> 
>> On May 22, 2013, at 6:16 PM, Jacques Hausser wrote:
>> 
> 
> 
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode




More information about the use-livecode mailing list