Code snippets for the blog post "A Math-er of fact"
Last active
August 15, 2016 22:50
-
-
Save warren-gavin/52ff9022295a95257543fa343e44b040 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
private func random(_ maxValue: Int) -> Int { | |
return Int(arc4random_uniform(UInt32(maxValue))) | |
} | |
func uniformRandom(_ range: CountableRange<Int>) -> Int { | |
return range.startIndex + random(range.endIndex - range.startIndex) | |
} | |
func skewedRandom(_ range: CountableRange<Int>) -> Int { | |
let x = random(range.endIndex - range.startIndex) | |
return range.startIndex + Int(Double(range.endIndex) * atan(Double(x))/M_PI_2) | |
} |
This file contains hidden or 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
let sampleSize = 30 | |
let range = 0 ..< 10 | |
var numbers = Array(0 ..< sampleSize).map({ _ in uniformRandom(range) }).sorted() | |
print("uniform distribution \(numbers)") | |
numbers = Array(0 ..< sampleSize).map({ _ in skewedRandom(range) }).sorted() | |
print("skewed distribution \(numbers)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment