keen/random/util

Source
random bool() random
random-chance bool(numerator nat32, denominator nat32) random
Returns true with probability numerator / denominator
random[e] e() random, e enum-like
random nat8() random
random nat8(max-exclusive nat8) random
random nat16() random
random nat16(max-exclusive nat16) random
random nat32() random
random nat32(max-exclusive nat32) random
random nat64() random
random nat64(max-exclusive nat64) random
random float32(max-exclusive float32) random
random float32(range float32 range) random
random float64(max-exclusive float64) random
random float64(range float64 range) random
random[t] t(choices (t, t)) random
random[col, elem] elem(choices col) random, (col, elem) array-like
shuffle[out, col, elem] out(a col) random, col size, (col, elem) some, (out, elem) list-new
shuffle[t] void(a t buffer) random
shuffle[t] void(a t mut[]) random
random-derangement[out, col, elem] out(a col) random, (col, elem) array-like, (out, elem) list-new
Randomly shuffles the array but ensures each element does not end up where it started
random-cycle[out, col, elem] out(a col) random, col size, (col, elem) some, (out, elem) list-new
Randomly shuffles the array but ensures the permutation is a cycle, meaning if you follow the old -> new position of each element, you go all the way through.
random-cycle[t] void(a t buffer) random
remove-random[t] t option(a t mut[]) random
remove-random[t] (t, t[]) option(a t[]) random
randoms-with-replacement[col, elem] elem[](choices col, n nat32) random, (col, elem) array-like
Chooses a random element from choices n times. The choices are unrelated and the same element may be chosen multiple times.
randoms-with-replacement-where[col, elem] elem[] option(choices col, n nat32, f bool mut(elem)) random, (col, elem) array-like
This is equivalent to but more efficient than than calling random-where in a loop. If f rejects every choice, this returns an empty option. Otherwise the result size will be n. This avoids calling f twice on the same element.
random-bag[t] record mut
is-shuffled bool
values t mut[]
Bag of elements that can be taken out at random. Supports replacing or not replacing the values. For most use cases, using randoms-with-replacement or randoms-without-replacement is simpler.
list-new[t] t random-bag(values t[])
any[t] bool(a t random-bag)
size[t] nat32(a t random-bag)
~=[t] void(a t random-bag, value t) random
take-random[t] t option(a t random-bag) random
Gets a random value and removes it from the bag
get-random[t] t option(a t random-bag) random
This does not remove the value.
take-and-maybe-replace[t] t option(a t random-bag, replace bool mut(t)) random
If replace returns true, this will act like get-random, otherwise it will act like take-random.
randoms-without-replacement[col, elem] elem[](choices col, n nat32) random, (col, elem) array-like
This may return less than n elements if choices is not big enough.
randoms-without-replacement-where[col, elem] elem[](choices col, n nat32, f bool mut(elem)) random, (col, elem) array-like
This may return less than n elements if there aren't that many that satisfy f.
random-indices-without-replacement nat32[](size nat32, n nat32) random
random-indices-without-replacement-where[col, elem] nat32[](choices col, n nat32, f bool mut(elem)) random, (col, elem) array-like
WARN: This may return less than n indices if there aren't that many that satisfy f.
random-where[col, elem] elem option(choices col, f bool mut(elem)) random, (col, elem) array-like
Returns an empty option if f rejected every choice.
first-random-where[out, col, elem] out option(a col, f out option mut(elem)) (col, elem) array-like, random
Equivalent to first a.shuffle, f
random-index-where[col, elem] nat32 option(choices col, f bool mut(elem)) random, (col, elem) array-like
Returns an empty option if f rejected every choice. This minimizes calls to f. f will only be called so long as it rejects choices and will not be asked twice about the same choice.
weighted-choice record (has private fields)
size nat32(a weighted-choice)
weighted-choice weighted-choice(weights nat32[])
Preprocesses weights so that we can quickly make weighted random choices. Conceptually, this is like repeating each choice by its weight, then choosing a random element from the resulting array. For the implementation, this uses the Walker alias method.
random-index nat32(a weighted-choice) random
random[t] t(a weighted-choice, values t[]) random
Chooses a value using random-index.