Functions
inline fn enumValue(r: Random, comptime EnumType: type) EnumType
Returns a random value from an enum, evenly distributed.
Returns a random value from an enum, evenly distributed.
Note that this will not yield consistent results across all targets due to dependence on the representation of
usize
as an index. SeeenumValueWithIndex
for further commentary.fn enumValueWithIndex(r: Random, comptime EnumType: type, comptime Index: type) EnumType
Returns a random value from an enum, evenly distributed.
Returns a random value from an enum, evenly distributed.
An index into an array of all named values is generated using the specified
Index
type to determine the return value. This allows for results to be independent ofusize
representation.Prefer
enumValue
if this isn’t important.See
uintLessThan
, which this function uses in most cases, for commentary on the runtime of this function.fn float(r: Random, comptime T: type) T
Return a floating point value evenly distributed in the range [0, 1).
fn floatExp(r: Random, comptime T: type) T
Return an exponentially distributed float with a rate parameter of 1.
Return an exponentially distributed float with a rate parameter of 1.
To use a different rate parameter, use: floatExp(…) / desiredRate.
fn floatNorm(r: Random, comptime T: type) T
Return a floating point value normally distributed with mean = 0, stddev = 1.
Return a floating point value normally distributed with mean = 0, stddev = 1.
To use different parameters, use: floatNorm(…) * desiredStddev + desiredMean.
fn init(pointer: anytype, comptime fillFn: fn (@TypeOf(pointer), []u8) void) Random
No documentation provided.
fn int(r: Random, comptime T: type) T
Returns a random int
i
such thatminInt(T) <= i <= maxInt(T)
.i
is evenly…Returns a random int
i
such thatminInt(T) <= i <= maxInt(T)
.i
is evenly distributed.fn intRangeAtMost(r: Random, comptime T: type, at_least: T, at_most: T) T
Returns an evenly distributed random integer
at_least <= i <= at_most
. See `u…Returns an evenly distributed random integer
at_least <= i <= at_most
. SeeuintLessThan
, which this function uses in most cases, for commentary on the runtime of this function.fn intRangeAtMostBiased(r: Random, comptime T: type, at_least: T, at_most: T) T
Constant-time implementation off
intRangeAtMostBiased
. The results of this fu…Constant-time implementation off
intRangeAtMostBiased
. The results of this function may be biased.fn intRangeLessThan(r: Random, comptime T: type, at_least: T, less_than: T) T
Returns an evenly distributed random integer
at_least <= i < less_than
. See `…Returns an evenly distributed random integer
at_least <= i < less_than
. SeeuintLessThan
, which this function uses in most cases, for commentary on the runtime of this function.fn intRangeLessThanBiased(r: Random, comptime T: type, at_least: T, less_than: T) T
Constant-time implementation off
intRangeLessThan
. The results of this functi…Constant-time implementation off
intRangeLessThan
. The results of this function may be biased.inline fn shuffle(r: Random, comptime T: type, buf: []T) void
Shuffle a slice into a random order.
Shuffle a slice into a random order.
Note that this will not yield consistent results across all targets due to dependence on the representation of
usize
as an index. SeeshuffleWithIndex
for further commentary.fn shuffleWithIndex(r: Random, comptime T: type, buf: []T, comptime Index: type) void
Shuffle a slice into a random order, using an index of a specified type to main…
Shuffle a slice into a random order, using an index of a specified type to maintain distribution across targets. Asserts the index type can represent
buf.len
.Indexes into the slice are generated using the specified
Index
type, which determines distribution properties. This allows for results to be independent ofusize
representation.Prefer
shuffle
if this isn’t important.See
intRangeLessThan
, which this function uses, for commentary on the runtime of this function.fn uintAtMost(r: Random, comptime T: type, at_most: T) T
Returns an evenly distributed random unsigned integer
0 <= i <= at_most
. See …Returns an evenly distributed random unsigned integer
0 <= i <= at_most
. SeeuintLessThan
, which this function uses in most cases, for commentary on the runtime of this function.fn uintAtMostBiased(r: Random, comptime T: type, at_most: T) T
Constant-time implementation off
uintAtMost
. The results of this function may…Constant-time implementation off
uintAtMost
. The results of this function may be biased.fn uintLessThan(r: Random, comptime T: type, less_than: T) T
Returns an evenly distributed random unsigned integer
0 <= i < less_than
. Thi…Returns an evenly distributed random unsigned integer
0 <= i < less_than
. This function assumes that the underlyingfillFn
produces evenly distributed values. Within this assumption, the runtime of this function is exponentially distributed. IffillFn
were backed by a true random generator, the runtime of this function would technically be unbounded. However, iffillFn
is backed by any evenly distributed pseudo random number generator, this function is guaranteed to return. If you need deterministic runtime bounds, useuintLessThanBiased
.fn uintLessThanBiased(r: Random, comptime T: type, less_than: T) T
Constant-time implementation off
uintLessThan
. The results of this function m…Constant-time implementation off
uintLessThan
. The results of this function may be biased.fn weightedIndex(r: std.rand.Random, comptime T: type, proportions: []const T) usize
Randomly selects an index into
proportions
, where the likelihood of each inde…Randomly selects an index into
proportions
, where the likelihood of each index is weighted by that proportion. It is more likely for the index of the last proportion to be returned than the index of the first proportion in the slice, and vice versa.This is useful for selecting an item from a slice where weights are not equal.
T
must be a numeric type capable of holding the sum ofproportions
.