01. Random Number Generator
Description
Description: Generates a random number between 0 and 99.
Code
IF ANSWERED( Random-n ) = FALSE
DAY OF( TODAY )
ELSE
Random-n
END IF
POWER( RESULT , 0.33 )
IF RESULT > 4
RESULT - 4
ELSE IF RESULT > 3
RESULT - 3
ELSE IF RESULT > 2
RESULT - 2
ELSE IF RESULT > 1
RESULT - 1
END IF
RESULT * 100
IF RESULT = 0
100
END IF
SET Random-n TO RESULT
TRUNCATE( RESULT, 0 )
Explanation
The computation requires one variable, Random-n -- a number variable used to store the last number generated to seed the next "random" number.
Here's the philosophy. If you simply use some combination of the date, or the length of a string, you can't use the "random" number more than once in a single assembly run since it will produce the same result every time. Instead, we need some kind of computation that will produce an original number every time it is invoked, even in the same assembly run.
This code accomplishes that. It takes a number between 1 and 100 and gets the 0.33 power of it. This produces an ugly number less than 5 with lots of decimal places. I figured that the real variety was in the decimal places, so I used a series of IF/ELSE IF statements to trim off the 1's number (i.e. get the number trimmed down to just its decimals). This decimal is then multiplied by 100 to produce a number between 0 and 100 (with many trailing decimals). If the result is 0, it is set to 100.
The randomness is produced by never having the same number plugged into the computation twice. The first time the computation runs it will use the date for its plug-in. The output of the computation is stored in the variable Random-n. Thenceforth the current value of Random-n will be plugged in each time the computation is run. We don't store the truncated version of the output because the variety comes from the decimals.
After saving the random number (with its decimals) to seed the next run, we truncate the number, returning just the integer portion. This will always be between 1 and 100.
A test run of 500 computations hit every number between 0 and 99 an average of 5 times in an acceptably random order.