Standard Library: Generators
alpha
[alpha: count ? 1]
→ string
Prints a uniformly random lowercase alphabetic string.
dig
[dig: count ? 1]
→ string
Prints a uniformly random decimal digit. If count is specified, repeats count times.
Parameters
count ← int (optional)
The number of digits to generate. Defaults to 1.
Example
# Generate a random 32-character decimal string
[dig:32] # ~> 01952208554533821061510695429126
digh
[digh: count ? 1]
→ string
Prints a uniformly random lowercase hexadecimal digit. If count is specified, repeats count times.
Parameters
count ← int (optional)
The number of digits to generate. Defaults to 1.
Example
# Generate a random 32-character hex string
[digh:32] # ~> f4e5bef31ea02eac22f220e68e837587
dignz
[dignz: count ? 1]
→ string
Prints a uniformly random non-zero decimal digit. If count is specified, repeats count times.
Parameters
count ← int (optional)
The number of digits to generate. Defaults to 1.
Example
# Generate a random 32-character decimal string without zeros
[dignz:32] # ~> 92558761934966287236132511739511
maybe
[maybe: p ? 0.5]
→ bool
Returns a bool value with p probability of being true.
p must be either a float or nothing. If omitted, it will default to 0.5.
Parameters
p ← float | nothing (optional)
The probability (0.0 <= p <= 1.0) of the printed boolean being true.
If omitted or nothing, defaults to 0.5.
pick
[pick: collection]
Prints a random element from an ordered collection.
pickn
[pickn: collection; count]
→ list
Prints a list containing count random elements sampled from collection.
pick-sparse
[pick-sparse: first; ...rest]
Randomly selects one value from a sparse weighted set of arguments by using each argument's length as its weight.
rand
[rand: a; b]
→ int
Prints a random integer with uniform distribution between a and b (both inclusive).
Parameters
a ← int
The first inclusive bound of the random number.
b ← int
The second inclusive bound of the random number.
Example
# Choose a number by fair dice roll.
You roll a `[rand:1;6].
# ~> You roll a 4.
randf
[randf: a; b]
→ float
Prints a random float with uniform distribution between a (inclusive) and b (exclusive).
Parameters
a ← float
The first inclusive bound of the random number.
b ← float
The second inclusive bound of the random number.
rand-list
[rand-list: a; b; n]
→ list
Prints a list of n random integers with uniform distribution between a and b (both inclusive).
Parameters
a ← int
The first inclusive bound of the random numbers.
b ← int
The second inclusive bound of the random numbers.
n ← int
The amount of numbers to generate.
Example
# 2-dice roll
<$roll = [rand-list: 1; 6; 2]>
You rolled `[join: \sand\s; <roll>] for a total of `[sum: <roll>].
# ~> You rolled 5 and 3 for a total of 8.
randf-list
[randf-list: a; b; n]
→ list
Prints a list of n random floats with uniform distribution between a (inclusive) and b (exclusive).
Parameters
a ← float
The first inclusive bound of the random numbers.
b ← float
The second inclusive bound of the random numbers.
n ← int
The amount of numbers to generate.
rand-list-sum
[rand-list-sum: input; count; variance]
→ list
Generates a list of count random numbers, whose sum equals input, and with a maximum absolute difference of variance.
Parameters
input ← int | float
The input number to that the output numbers should add up to.
count ← int
The amount of numbers to generate.
variance ← int | float
The maximum absolute difference between any two generated numbers.
Errors
Raises an error if count is less than 1.
Example
# Generate and print a list of 5 random numbers that add up to 1000
<$parts = [rand-list-sum: 1000; 5; 200]>
[join: <parts>; \s+\s] = [sum: <parts>]
##
Output:
170 + 378 + 83 + 189 + 180 = 1000
##