HJay,
It only seems like magic if you don't know what the man behind the curtain is doing. (Smile.) If there is some part of my code that you are having trouble following, please ask and I'll try to explain.
To get back to your original question of why you kept getting the same result every time you ran the handler. I believe it has to do with the fact that the LiveCode does not generate true random numbers. (This is true also of all other modern programming languages that I am aware of.) Computers are deterministic, and it's actually quite hard (some say impossible) for a computer to genereate true random numbers.
Instead, LiveCode uses a psuedo-random number generator that uses an algorithm to generate a string of apparently random numbers. The things that shocks most people new to programming is if you call the random function repeated in a handler, it generates exactly the same sequence of numbers each time. Developers try to pass this off as a "feature," and admittedly there are some times when this is useful -- for scientists, statistictians, and even programmers who are trying to debug a program and want to run the same series of random numbers over and over again while they tinker with other parts of the program to get it working right. But for most of us, it's a pain in the neck to deal with, and when it's working right, the generated psuedo-random numbers look so convincing that it's easy to forget that they're not truly random.
So every time you opened your stack (or clicked on a button if you've moved the script there), LiveCode started to generate the same series of numbers that it used the last time. (It does reset itself each time you close and re-open LiveCode itself.) What to do?
Well, I'm sure, like everything else in LiveCode, that there are multiple ways of dealing with this. I use a two-step process:
1) Just before I begin the coding section to generate a "random" number, I insert the following line of code:
set the randomSeed to the seconds This causes LiveCode to begin a new series of "random" numbers. You could set the randomSeed to almost any number, but using "the seconds" is a good choice since it is always a different number that the last time it was called (unless you call it again within one second -- and because LiveCode is so fast, you must be aware of this possibilty, in such a situtation you can set the randomSeed to the milliseconds instead. No code is likely to run twice within one millisecond).
2) I then generate all the random numbers I need in advance (using a repeat loop) and store them in a variable. Then I pull the random numbers from the variable as needed.
A few words about the last step. It would seem that instead of putting the numbers into a variable, you could just put them directly into a field (or wherever else you need them) as you generate them. However, for reasons that I've never seen clearly explained, the results you get doing this are not always reliable. This is not something new. Here for example is a quote from Rob Cozens from June 9, 2006: "Generally, if not always, evaluating the function into a variable and then referencing the variable works. I've never tracked down the "why"; but the fix [i.e., avoiding unpredicable results] seems to work reliably." (
http://www.mail-archive.com/use-revolut ... 79081.html) This is one of those cases where I don't understand what the man behind the curtain is doing, but at least I know how to work around it. Perhaps those more knowledgable about LiveCode (which is almost everyone in these forums) can enlighted us further about these matters.
Dealing with random numbers is not as trivial or as easy as it first appears, and if you do a search on the Internet, you'll see that it has been the subject of countless discussions. Given the rather frequent need for random numbers in programs, I'm suprised that the LiveCode User Guide and Dictionary don't give a fuller account of how best to use the random function and how to avoid the sort of problem you've been having.