Updated smnp.math (markdown)

Bartłomiej Przemysław Pluta
2020-03-25 20:22:34 +01:00
parent feff6c34b2
commit 03ff7a1242

@@ -1,11 +1,65 @@
# Functions # Functions
## `random(min: int, max: int)` ## `random(min: int, max: int)`
Returns a random `int` from a given range _exclusively_.
## `random(min: float, max: float)` ## `random(min: float, max: float)`
Returns a random `float` from a given range _exclusively_.
## `min(numbers: list<int, float>)` ## `min(numbers: list<int, float>)`
Returns the smallest number of given list.
## `max(numbers: list<int, float>)` ## `max(numbers: list<int, float>)`
Returns the greatest number of given list.
## `sample(list: list)` ## `sample(list: list)`
Returns a random item of given list.
## `pick(items: list<map<string><>>)` ## `pick(items: list<map<string><>>)`
Returns a random item of given list using a configuration maps. It is a more generic version of `sample()` function.
### Arguments
* `items` - list of configuration maps. Each map has to match following schema:
| 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
```
## `pick(...items: map<string><>)` ## `pick(...items: map<string><>)`
See `pick(items: list<map<string><>>)`
## `mod(a: int, b: int)` ## `mod(a: int, b: int)`
## `range(begin: int, end: int)` Returns the remainder after division of `a` by `b`.
## `random()`
## `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]
```
## `random()`
Gets the random `float` value uniformly distributed between 0 (inclusive) and 1 (exclusive).