diff options
| author | Veikka Tuominen <git@vexu.eu> | 2022-08-30 13:02:17 +0300 |
|---|---|---|
| committer | Veikka Tuominen <git@vexu.eu> | 2022-08-30 13:02:17 +0300 |
| commit | e6be6d97683d5443f48ce8b6d86fdf27f0f1d556 (patch) | |
| tree | 7348ad6c9a7816dd618c7053e1addc7bf27c717d /lib/std | |
| parent | 527055a8215b23a13fb407bdeba2908b8206f6f0 (diff) | |
| download | zig-e6be6d97683d5443f48ce8b6d86fdf27f0f1d556.tar.gz zig-e6be6d97683d5443f48ce8b6d86fdf27f0f1d556.zip | |
std.rand: make weightedIndex proportions param a const slice
The function does not mutate the proportions and the signature should reflect that.
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/rand.zig | 2 | ||||
| -rw-r--r-- | lib/std/rand/test.zig | 2 |
2 files changed, 2 insertions, 2 deletions
diff --git a/lib/std/rand.zig b/lib/std/rand.zig index c13d2895c9..e94578c119 100644 --- a/lib/std/rand.zig +++ b/lib/std/rand.zig @@ -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. diff --git a/lib/std/rand/test.zig b/lib/std/rand/test.zig index cae77d6e37..1ad9adbeb8 100644 --- a/lib/std/rand/test.zig +++ b/lib/std/rand/test.zig @@ -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; |
