विवरण
Xcode: 9.2, स्विफ्ट 4
उपाय
extension BinaryInteger {
static func rand(_ min: Self, _ max: Self) -> Self {
let _min = min
let difference = max+1 - _min
return Self(arc4random_uniform(UInt32(difference))) + _min
}
}
extension BinaryFloatingPoint {
private func toInt() -> Int {
if let value = self as? CGFloat {
return Int(value)
}
return Int(self)
}
static func rand(_ min: Self, _ max: Self, precision: Int) -> Self {
if precision == 0 {
let min = min.rounded(.down).toInt()
let max = max.rounded(.down).toInt()
return Self(Int.rand(min, max))
}
let delta = max - min
let maxFloatPart = Self(pow(10.0, Double(precision)))
let maxIntegerPart = (delta * maxFloatPart).rounded(.down).toInt()
let randomValue = Int.rand(0, maxIntegerPart)
let result = min + Self(randomValue)/maxFloatPart
return Self((result*maxFloatPart).toInt())/maxFloatPart
}
}
प्रयोग
print("\(Int.rand(1, 20))")
print("\(Float.rand(5.231233, 44.5, precision: 3))")
print("\(Double.rand(5.231233, 44.5, precision: 4))")
print("\(CGFloat.rand(5.231233, 44.5, precision: 6))")
पूरा नमूना
import Foundation
import CoreGraphics
func run() {
let min = 2.38945
let max = 2.39865
for _ in 0...100 {
let precision = Int.rand(0, 5)
print("Precision: \(precision)")
floatSample(min: Float(min), max: Float(max), precision: precision)
floatSample(min: Double(min), max: Double(max), precision: precision)
floatSample(min: CGFloat(min), max: CGFloat(max), precision: precision)
intSample(min: Int(1), max: Int(10000))
print("")
}
}
private func printResult<T: Comparable>(min: T, max: T, random: T) {
let result = "\(T.self) rand[\(min), \(max)] = \(random)"
print(result)
}
func floatSample<T: BinaryFloatingPoint>(min: T, max: T, precision: Int) {
printResult(min: min, max: max, random: T.rand(min, max, precision: precision))
}
func intSample<T: BinaryInteger>(min: T, max: T) {
printResult(min: min, max: max, random: T.rand(min, max))
}
परिणाम