Skip to content

Instantly share code, notes, and snippets.

@ihomway
Created July 9, 2019 08:10
Show Gist options
  • Save ihomway/7249cba1dc75e8d944696b6170a723ea to your computer and use it in GitHub Desktop.
Save ihomway/7249cba1dc75e8d944696b6170a723ea to your computer and use it in GitHub Desktop.
linear congruential generator
protocol RandomNumberGenerator {
func random() -> Double
}
class LinearConguentialGenerator: RandomNumberGenerator {
var lastRandom = 42
let m = 139968.0
let a = 3877.0
let c = 29573.0
func random() -> Double {
lastRandom = ((lastRandom * a + c).truncatingRemainder(dividingBy:m))
return lastRandom / m
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment