rand.util: add sample_r and sample_nr (#8539)

This commit is contained in:
Subhomoy Haldar 2021-02-04 18:26:53 +05:30 committed by GitHub
parent a976876211
commit c6552d7780
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 0 deletions

View file

@ -0,0 +1,31 @@
import rand.util
fn test_sample_nr() {
lengths := [1, 3, 4, 5, 6, 7]
a := ['one', 'two', 'three', 'four', 'five', 'six', 'seven']
for length in lengths {
b := util.sample_nr(a, length)
assert b.len == length
for element in b {
assert element in a
// make sure every element occurs once
mut count := 0
for e in b {
if e == element {
count++
}
}
assert count == 1
}
}
}
fn test_sample_r() {
k := 20
a := ['heads', 'tails']
b := util.sample_r(a, k)
assert b.len == k
for element in b {
assert element in a
}
}