5
smnp.math
Bartłomiej Przemysław Pluta edited this page 2020-04-02 09:15:49 +02:00

Functions

random

random(min: int, max: int)     #1
random(min: float, max: float) #2
random()                       #3

Returns a random number from a given range exclusively (#1, #2). Returns the random float value uniformly distributed between 0 (inclusive) and 1 (exclusive) if no argument passed (#3).

Output

  • int if passed arguments are of int type
  • float if passed arguments are of float type

min

min(numbers: list<int, float>)

Returns the smallest number of given list.

max

max(numbers: list<int, float>)

Returns the greatest number of given list.

sample

sample(list: list)

Returns a random item of given list.

pick

pick(items: list<map<string><>>)
pick(...items: map<string><>)

Returns a random item of given list using a configuration maps. It is a more generic version of smnp.math::sample function.

Arguments

  • items - list of configuration maps. Each map has to match following schema (all keys are required):
KEY VALUE
chance An int value which determines the percentage (0-100) chances to draw this item.
value An value to be returned if this item will be drawn.

The sum of chance value of each item should be equal to 100.

Example

items = [
    { chance -> 70, value -> "70%" },
    { chance -> 25, value -> "25%" },
    { chance -> 5, value -> "5%" }
];

collected = 100000 ^ pick(items);

println("70% : ", collected.countBy("70%"));
println("25% : ", collected.countBy("25%"));
println("5% : ", collected.countBy("5%"));

# Output:
# 75% : 70214
# 25% : 24800
# 5% : 4986

mod

mod(a: int, b: int)

Returns the remainder after division of a by b.

range

range(begin: int, end: int, step: int = 1)

Returns a list contained of integers from begin (inclusive) to end (exclusive) with given step.

Example

println(range(4, 20, 2));    # [4, 6, 8, 10, 12, 14, 16, 18]