Hi all, this is (hopefully) a quick tutorial allowing you to set a bias of random options you might have. The example .capx file can be got : Here
We're are going to use:
An array : This will store the bias for the random options, putting them in an array allows us to change at runtime (for things like level progression)
A Global String : This variable will store the number chances set by the array.
TokenAt/TokenCount : Using this to pick a value from the Global String.
Setting it Up
Start by making an Array called “NumberChances” and set with to 10 (or whatever number of chances you wish) and a Global STRING variable called “NumberChancesList”
Populating the Array
We’re going to start by setting up the array with the number chances. Repeat for all the chances you wish to set, by setting the X value of the array (which is the number we want to pick) to the value of chance of it being picked. For example 0 has a 1% chance of being picked by setting Array.At(0) to 1.
Global text NumberChanceList = “”
System > On start of Layout | NumberChances | Set Value at 0 to 1
NumberChances | Set Value at 1 to 2
etc....
etc....
Populating the String
Now we need to populate the NumberChanceList variable from the array, you can either do this at/on load or in a function.
We start by clearing the NumberChanceList variable, and repeat the width of the array for each loop of that repeat we want to know the loopindex so that we can add that value to are variable (remember the array X is the number we want to retrieve).
Create a Local integer variable called “number” set initial value to 0. Now do:
System | set NumberChancesList to “”
Local number = 0
System Repeat NumberChances.Width times | Set number to loopindex
For each loop we now do another loop to repeat to number set at X in our array and then adding the value of number to NumberChancesList with a “,” separating the values.
Final Set up for String Population
Final loops look like this :
System | set NumberChancesList to “”
Local number = 0
System Repeat NumberChances.Width times | Set number to loopindex
System Repeat NumberChances.At(number) times | System Set NumberChanceList to NumberChanceList & number & “,”
Getting Random Bias
Now all we have to do is pick a number at random from NumberChanceList because the numbers in string have been weighted according to our array.
To get a random number use :
int( tokenat( NumberChancesList, int( random( tokencount( NumberChancesList, ",") ) ) , ",") )
That’s it, you can now use that to get a Biased Random Number
Full view of the structure
Global text NumberChanceList = “”
System > On start of Layout | NumberChances | Set Value at 0 to 1
NumberChances | Set Value at 1 to 2
etc....
etc....
System | set NumberChancesList to “”
Local number = 0
System Repeat NumberChances.Width times | Set number to loopindex
System Repeat NumberChances.At(number) times | System Set NumberChanceList to NumberChanceList & number & “,”
int( tokenat( NumberChancesList, int( random( tokencount( NumberChancesList, ",") ) ) , ",") )