randomly order a list
ambassador at fourthworld.com
Thu May 23 15:40:31 EDT 2013
Björnke von Gierke wrote:
> sort the lines of theList by random(the number of lines in theList)
> This too works. there's no need to make an arbitrary large number
> _BECAUSE IT MIGHT IN SOME CASES DECREASE RANDOMNESS_. Mostly when
> your variable grows bigger then anticipated, so usually you're fine.
> But still, don't use arbitrary magic numbers, it's just bad coding
Surprisingly, it does seem to improve the distribution of results to use
a large value in the argument to random.
While waiting for a backup here I took a couple minutes to test the
options presented in this discussion in a stack:
go url "http://fourthworld.net/lc/RandomSort.rev"
It turns out that both my crude manual-like method and using a large
value for random produce far more even distributions of results than
using random with the number of lines in the list.
In fact, as you'll see in that stack (unless I goofed in tossing it
together so quickly - please check the card script for the functions and
let me know if I screwed up) it seems that we get the same results
scoring most-frequent and less-frequent when using the number of lines
of the list, while the other two methods vary the most- and
least-frequent pretty much evenly across the range of possible solutions.
I'm not sure why this is the case, and while I'm reluctant to call it a
"bug" it may well be an "anomaly". :)
But by any name, useful to know.
And for those perverse souls obsessed with performance, the crude method
I posted earlier also turns out to be ever so slightly faster than using
the random function. The difference isn't significant, and of course
random is the better choice for longer lists, but once again it reminds
us that LiveCode is seductive this way, that the number of lines we type
rarely tells us much about how hard the engine needs to work under the
hood. RegEx vs. pull parsers is another example, and there are many
others to discover for when performance is critical.
> As for the original poster: DO NOT SET THE RANDOMSEED!!!!
...unless what you're looking for is a reproducible set of
random-seeming results. Setting the randomSeed to a given value will
deliver a reasonable distribution of results, but each time you run the
same code with the same randomSeed you'll get the exact same results.
If that's what you're after, it's useful to know, but of course for most
normal uses of the random function setting the randomSeed will not be
what you're after.
LiveCode training and consulting: http://www.fourthworld.com
Webzine for LiveCode developers: http://www.LiveCodeJournal.com
Follow me on Twitter: http://twitter.com/FourthWorldSys
More information about the Use-livecode