Created
September 8, 2020 00:39
-
-
Save aryamansharda/8bc6378a75685bd36face2ef2d656940 to your computer and use it in GitHub Desktop.
LinearCongruentialGenerator+Range.swift
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
// Simulating 1,000,000 random values in a given range | |
let upperBound: Double = 80 | |
let lowerBound: Double = 30 | |
let modulus = 2147483648 | |
var seed = 0 | |
for _ in 1...1000000 { | |
let randomNumber = LCG.generate(modulus: modulus, multiplier: 1103515245, increment: 12345, seed: seed) | |
seed = randomNumber | |
let scalar = Double(randomNumber) / Double(modulus - 1) | |
let mappedNumber = Int((upperBound - lowerBound) * scalar + lowerBound) | |
print(mappedNumber) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment