Sorting question

Brian Yennie briany at qldlearning.com
Wed Feb 10 23:32:27 CST 2010

```Craig,

Yes, the random function is called for each item to assign it a sort value. My guess as to why using a larger number in random(n) is as follows;

If you use random(3) there is a high chance that two items will be assigned the same value. In this case, the sort algorithm may preserve their original order, adding an unnatural likelihood that the items remain close to original order.

Better yet, I would use the largest number that random() will accept. Theoretically the best would be to supply your own random function that never repeats, or use a loop similar to what Jeff suggests.

> If I want to randomize a string of items using the random function as a
> sortKey, say:
>
> put "1,2,3" into temp
> sort items of temp by random(3)
>
> The randomization is not nearly as effective as if I say:
>
> put "1,2,3" into temp
> sort items of temp by random(100)
>
> If you run this many times to create a list of random strings (like "3,1,2"
> and "2,1,3", etc.) the second version randomizes much better than the
> first. This is a trick that harkens back to early HC. But it occurs to me I never
> really understood the rationale. In fact, I don't understand the syntactic
> methodology.
>
> This does not work at all:
>
> put "1,2,3" into temp
> get random(100)
> sort items of temp by it
>
> So the random function must be invoked for each item, item by item, as the
> process progresses. As a sortKey, this seems reasonable. But how does this
> operate, and what does the larger number do so much better?
>