Add sample(_) and sample(_,_) to Random.

This commit is contained in:
Bob Nystrom
2016-02-09 07:24:45 -08:00
parent d4a4b26203
commit 8b36e2f00b
11 changed files with 248 additions and 5 deletions

View File

@ -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