keen/random/util
Sourcerandom-derangement[out, col, elem] out(a col) random, (col, elem) array-like, (out, elem) list-newRandomly 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-newRandomly 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.
randoms-with-replacement[col, elem] elem[](choices col, n nat32) random, (col, elem) array-likeChooses 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-likeThis 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 mutvalues 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.randoms-without-replacement[col, elem] elem[](choices col, n nat32) random, (col, elem) array-likeThis 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-likeThis may return less than
n elements if there aren't that many that satisfy f.random-indices-without-replacement-where[col, elem] nat32[](choices col, n nat32, f bool mut(elem)) random, (col, elem) array-likeWARN: 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-likeReturns 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, randomEquivalent to
first a.shuffle, frandom-index-where[col, elem] nat32 option(choices col, f bool mut(elem)) random, (col, elem) array-likeReturns 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)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.
truewith probabilitynumerator/denominator