Randomization helper functions

Randomization helper functions v2.1

No permission to download
Content Author
The Mana World
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)

Author
meko
Downloads
25
Views
2,731
First release
Last update

Ratings

0.00 star(s) 0 ratings

More resources from meko

  • Area timer functions
    Area timer functions
    This script provides functions to add (or remove) timers to every player in the area or map.    ...
  • Hash table
    Hash table
    This plugin exposes the internal hash table (strdb) to the script engine to provide a key-value...
  • Array manipulation functions
    Array manipulation functions
    This script provides various array manipulation functions, and more might be added in the...
  • Date and Time functions
    Date and Time functions
    This script provides functions to easily calculate date and time. More functions might be added...
  • Safe string manipulation functions
    Safe string manipulation functions
    This script provides safe string manipulation functions that do not require PCRE to be enabled...

Latest updates

  1. Version v2.1

    fixed a few edge cases with relative_array_random()
  2. Version v2

    added relative_array_random()
Back
Top