mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-11 22:28:45 +01:00
Add sample(_) and sample(_,_) to Random.
This commit is contained in:
20
test/random/sample_one.wren
Normal file
20
test/random/sample_one.wren
Normal file
@ -0,0 +1,20 @@
|
||||
import "random" for Random
|
||||
|
||||
var random = Random.new(12345)
|
||||
|
||||
// Single element list.
|
||||
System.print(random.sample(["single"])) // expect: single
|
||||
|
||||
// Should choose all elements with roughly equal probability.
|
||||
var list = ["a", "b", "c", "d", "e"]
|
||||
var histogram = {"a": 0, "b": 0, "c": 0, "d": 0, "e": 0}
|
||||
for (i in 1..1000) {
|
||||
var sample = random.sample(list)
|
||||
histogram[sample] = histogram[sample] + 1
|
||||
}
|
||||
|
||||
System.print(histogram.count) // expect: 5
|
||||
for (key in histogram.keys) {
|
||||
var error = (histogram[key] / (1000 / list.count) - 1).abs
|
||||
if (error > 0.2) System.print("!!! %(error)")
|
||||
}
|
||||
Reference in New Issue
Block a user