Randomization helper functions

meko

Core Developers
Messages
363
Points
0
IRC Nickname
meko
Github
Helianthella
Emulator
Hercules
Client Version
ManaPlus







View File

Randomization helper functions




This script provides syntactic sugar for randomization.
Works fine with both strings and integers.

any(<arg>{, <arg>{, ...}})
> returns a random argument from the passed arguments

emotion(any(e_hmm, e_grat, e_yawn)); // do any of those 3 emotes


 
any_of(<array>)
> returns a random entry from the passed array
Code:
setarray(.@foo, 1, 2, 3, 4); // build the array

// ... later in the code:
mes(any_of(.@foo)); // print any number from the array



relative_array_random(<array: 0, {[value, probability]...}>{, <recalculate>})
a more powerful version of any_of(), which allows to assign arbitrary probabilities to every entry instead of being rand(size) for every one.
the first value of the array is reserved and must be 0, and every entry must be a tuple of [value, probability].
if the array is modified between invocations please pass true as second argument to re-calculate the total probability.
probabilities are arbitrary and can be any number between 1 and INT_MAX.
> returns the random entry

setarray(.@foo, 0, // <= always 0
111, 6,
222, 12, // <= [value, probability]
333, 20,
444, 4);

relative_array_random(.@foo); // => 333 (example, unpredictable)

--------------------------------------------------------------------------------------
This script was made by me, for The Mana World + Evol.
License: public domain (CC0)









 
Last edited by a moderator:
Back
Top