Skip to content

Instantly share code, notes, and snippets.

@mmertsock
Created August 7, 2023 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmertsock/d00fd6ac0e4a06ecdc9f106078315001 to your computer and use it in GitHub Desktop.
Save mmertsock/d00fd6ac0e4a06ecdc9f106078315001 to your computer and use it in GitHub Desktop.
Stroop RNG test
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