authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-08-30 13:02:17+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-08-30 13:02:17+03:00
loge6be6d97683d5443f48ce8b6d86fdf27f0f1d556
tree7348ad6c9a7816dd618c7053e1addc7bf27c717d
parent527055a8215b23a13fb407bdeba2908b8206f6f0

std.rand: make weightedIndex proportions param a const slice

The function does not mutate the proportions and the signature should reflect that.

2 files changed, 2 insertions(+), 2 deletions(-)

lib/std/rand.zig+1-1
...@@ -343,7 +343,7 @@ pub const Random = struct {...@@ -343,7 +343,7 @@ pub const Random = struct {
343 ///343 ///
344 /// This is useful for selecting an item from a slice where weights are not equal.344 /// This is useful for selecting an item from a slice where weights are not equal.
345 /// `T` must be a numeric type capable of holding the sum of `proportions`.345 /// `T` must be a numeric type capable of holding the sum of `proportions`.
346 pub fn weightedIndex(r: std.rand.Random, comptime T: type, proportions: []T) usize {346 pub fn weightedIndex(r: std.rand.Random, comptime T: type, proportions: []const T) usize {
347 // This implementation works by summing the proportions and picking a random347 // This implementation works by summing the proportions and picking a random
348 // point in [0, sum). We then loop over the proportions, accumulating348 // point in [0, sum). We then loop over the proportions, accumulating
349 // until our accumulator is greater than the random point.349 // until our accumulator is greater than the random point.
lib/std/rand/test.zig+1-1
...@@ -452,7 +452,7 @@ test "Random weightedIndex" {...@@ -452,7 +452,7 @@ test "Random weightedIndex" {
452 var prng = DefaultPrng.init(0);452 var prng = DefaultPrng.init(0);
453 const random = prng.random();453 const random = prng.random();
454454
455 var proportions = [_]T{ 2, 1, 1, 2 };455 const proportions = [_]T{ 2, 1, 1, 2 };
456 var counts = [_]f64{ 0, 0, 0, 0 };456 var counts = [_]f64{ 0, 0, 0, 0 };
457457
458 const n_trials: u64 = 10_000;458 const n_trials: u64 = 10_000;