mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-18 13:49:59 +01:00
Add sample(_) and sample(_,_) to Random.
This commit is contained in:
@ -12,12 +12,21 @@ list = [1]
|
||||
random.shuffle(list)
|
||||
System.print(list) // expect: [1]
|
||||
|
||||
// Given enough tries, should generate all permutations.
|
||||
var hits = {}
|
||||
for (i in 1..200) {
|
||||
// Given enough tries, should generate all permutations with roughly equal
|
||||
// probability.
|
||||
var histogram = {}
|
||||
for (i in 1..5000) {
|
||||
var list = [1, 2, 3, 4]
|
||||
random.shuffle(list)
|
||||
hits[list.toString] = true
|
||||
|
||||
var string = list.toString
|
||||
if (!histogram.containsKey(string)) histogram[string] = 0
|
||||
histogram[string] = histogram[string] + 1
|
||||
}
|
||||
|
||||
System.print(histogram.count) // expect: 24
|
||||
for (key in histogram.keys) {
|
||||
var error = (histogram[key] / (5000 / 24) - 1).abs
|
||||
if (error > 0.2) System.print("!!! %(error)")
|
||||
}
|
||||
|
||||
System.print(hits.count) // expect: 24
|
||||
|
||||
Reference in New Issue
Block a user