1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

rand: simplify rand.PRNG, move to optional types for error handling (#13570)

This commit is contained in:
Subhomoy Haldar
2022-02-23 16:06:14 +05:30
committed by GitHub
parent 5c0b7b0d05
commit 114a341f5f
49 changed files with 609 additions and 1586 deletions

View File

@ -30,7 +30,7 @@ pub fn sample_r<T>(array []T, k int) []T {
n := array.len
mut results := []T{len: k}
for i in 0 .. k {
results[i] = array[rand.intn(n)]
results[i] = array[rand.intn(n) or { 0 }]
}
return results
}
@ -43,7 +43,7 @@ pub fn shuffle<T>(mut a []T, n int) {
}
cnt := if n == 0 { a.len - 1 } else { n }
for i in 0 .. cnt {
x := rand.int_in_range(i, a.len)
x := rand.int_in_range(i, a.len) or { 0 }
// swap
a_i := a[i]
a[i] = a[x]