Last active
September 8, 2020 04:10
-
-
Save aryamansharda/d53fb9e8dd5504a920ca8adec193e818 to your computer and use it in GitHub Desktop.
Linear Congruential Generator
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 | |
final class LCG { | |
static func generate(modulus: Int, multiplier: Int, increment: Int, seed: Int) -> Int { | |
return (multiplier * seed + increment) % modulus | |
} | |
} | |
// Generating 100 random numbers using LCG | |
var seed = 0 | |
for _ in 0..<100 { | |
let randomNumber = LCG.generate(modulus: 2147483648, multiplier: 1103515245, increment: 12345, seed: seed) | |
seed = randomNumber | |
print(randomNumber) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment