Created
August 7, 2023 17:44
-
-
Save mmertsock/d00fd6ac0e4a06ecdc9f106078315001 to your computer and use it in GitHub Desktop.
Stroop RNG test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
func tally(prob: Double, precision: UInt32, maxDiff: Double, repetitions: Int) { | |
var trues = 0 | |
let threshold = UInt32(prob * Double(precision)) | |
print("tally(prob: \(prob), precision: \(precision), repetitions: \(repetitions)):") | |
for _ in 1...repetitions { | |
let isTrue = arc4random_uniform(precision) < threshold | |
if isTrue { | |
trues += 1 | |
} | |
} | |
let actual = Double(trues) / Double(repetitions) | |
let diff = abs(actual - prob) | |
let exceeds = diff >= maxDiff | |
print("- trues: \(trues) / \(repetitions) == \(actual)% diff=\(diff) \(exceeds ? "EXCEEDS!" : "ok")\n") | |
} | |
for _ in 1...10 { | |
tally(prob: 0.25, precision: 100, maxDiff: 0.01, repetitions: 10_000) | |
tally(prob: 0.25, precision: 1_000, maxDiff: 0.01, repetitions: 10_000) | |
tally(prob: 0.25, precision: 10_000, maxDiff: 0.01, repetitions: 10_000) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment