std.rand: make weightedIndex proportions param a const slice

The function does not mutate the proportions and the signature should reflect that.
This commit is contained in:
Veikka Tuominen 2022-08-30 13:02:17 +03:00
parent 527055a821
commit e6be6d9768
2 changed files with 2 additions and 2 deletions

View File

@ -343,7 +343,7 @@ pub const Random = struct {
///
/// 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 of `proportions`.
pub fn weightedIndex(r: std.rand.Random, comptime T: type, proportions: []T) usize {
pub fn weightedIndex(r: std.rand.Random, comptime T: type, proportions: []const T) usize {
// This implementation works by summing the proportions and picking a random
// point in [0, sum). We then loop over the proportions, accumulating
// until our accumulator is greater than the random point.

View File

@ -452,7 +452,7 @@ test "Random weightedIndex" {
var prng = DefaultPrng.init(0);
const random = prng.random();
var proportions = [_]T{ 2, 1, 1, 2 };
const proportions = [_]T{ 2, 1, 1, 2 };
var counts = [_]f64{ 0, 0, 0, 0 };
const n_trials: u64 = 10_000;