Module random
Seedable RNG and helper functions.
Functions
irandom(min, max) | Return a random integer between min and max, inclusive. |
random(min, max) | Return a random number between min and max, inclusive. |
random.seed(seed) | Seed the RNG. |
random.genseed() | Generate a random seed for the RNG. |
random.prob(n) | Return true with probability n %, otherwise return false . |
random.choice(...) | Select one option uniformly from the provided list. |
random.removeChoice(opts) | Select and remove one option uniformly from the provided list. |
random.weighted(opts[, raw=false]) | Select one option from the keys of opts, with weight according to the values. |
random.removeWeighted(opts[, raw=false]) | Select and remove one option from the keys of opts, with weight according to the values. |
random.select(opts) | Select one option from opts, either as a list or as a table of value-weight pairs depending on opts. |
random.skewToWeights(skew, opts[, middle=0.0]) | Combine a skew factor with a list of options to produce a weighted set of options. |
random.sampleDiscRadius([inner], outer) | Generate a random radius on a disc or ring such that the resulting distribution, when combined with a uniform random angle, is uniform in the disc's area. |
random.sampleDisc([inner], outer) | Generate a uniform random point on a disc or ring. |
Functions
- irandom(min, max)
-
Return a random integer between min and max, inclusive.
This function matches the behaviour of the standard EmptyEpsilon irandom function, but uses the seedable RNG provided by this module.
Parameters:
- min integer The minimum value to generate.
- max integer The maximum value to generate.
Returns:
-
a random integer within the provided range.
- random(min, max)
-
Return a random number between min and max, inclusive.
This function matches the behaviour of the standard EmptyEpsilon random function, but uses the seedable RNG provided by this module.
Parameters:
- min number The minimum value to generate.
- max number The maximum value to generate.
Returns:
-
a random number within the provided range.
- random.seed(seed)
-
Seed the RNG.
Parameters:
- seed integer The seed to set.
- random.genseed()
-
Generate a random seed for the RNG.
Returns:
-
A seed suitable for passing to random.seed.
- random.prob(n)
-
Return
true
with probabilityn
%, otherwise returnfalse
.Parameters:
- n
integer
The probability to return
true
.
Returns:
-
boolean
true
,n
% of the time; otherwisefalse
. - n
integer
The probability to return
- random.choice(...)
-
Select one option uniformly from the provided list.
Parameters:
- ... The options to select from, or a single table argument containing the options to select from.
Returns:
-
a random entry from the provided list.
- random.removeChoice(opts)
-
Select and remove one option uniformly from the provided list.
Parameters:
- opts table A table to select and remove an entry from.
Returns:
-
a random entry from the provided list.
- random.weighted(opts[, raw=false])
-
Select one option from the keys of opts, with weight according to the values.
Passing
opts
={ x=1, y=2, [{"z", 42}]=4 }
will return:- with probability 1/7,
"x"
- with probability 2/7,
"y"
- with probability 4/7, either
"z", 42
(raw=false) or{"z", 42}
(raw=true)
Parameters:
- opts table The options to select from.
- raw boolean Whether to return table results without unpacking. (default false)
Returns:
-
a random key from the provided list.
- with probability 1/7,
- random.removeWeighted(opts[, raw=false])
-
Select and remove one option from the keys of opts, with weight according to the values.
For the interpretation of
opts
, see random.weighted.Parameters:
- opts table The options to select from.
- raw boolean Whether to unpack table results (false) or not (true). (default false)
Returns:
-
a random key from the provided list.
- random.select(opts)
-
Select one option from opts, either as a list or as a table of value-weight pairs depending on opts.
If all keys in opts are consecutive integers, then this function behaves as random.choice.
Otherwise, it behaves as random.weighted.
Parameters:
- opts
- random.skewToWeights(skew, opts[, middle=0.0])
-
Combine a skew factor with a list of options to produce a weighted set of options.
Parameters:
- skew number The skew amount to apply. Positive numbers bias the results towards higher indexes; negative numbers have the same effect towards lower ones.
- opts table The list of options to apply the skew factor to.
- middle number The middle-bias to apply. Positive numbers bias the results towards the middle of the list; negative numbers have the same effect towards the ends. (default 0.0)
Returns:
-
table
A table suitable for passing to random.weighted.
- random.sampleDiscRadius([inner], outer)
-
Generate a random radius on a disc or ring such that the resulting distribution, when combined with a uniform random angle, is uniform in the disc's area.
Parameters:
- inner number The inner radius of the ring. (optional)
- outer number The (outer) radius of the disc or ring.
Returns:
-
number
The resulting radius.
- random.sampleDisc([inner], outer)
-
Generate a uniform random point on a disc or ring.
Parameters:
- inner number The inner radius of the ring. (optional)
- outer number The (outer) radius of the disc or ring.
Returns:
- number The x coordinate of the result.
- number The y coordinate of the result.