Jump to content
meko

Randomization helper functions

Recommended Posts

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
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)


 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.