Created
December 27, 2019 08:50
-
-
Save dagronf/db753b330cd05d6304f7630961ac572f to your computer and use it in GitHub Desktop.
Generate an array of random values for Swift value types that support random value generation
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
/// Generate an array of random values for Swift value types that support random value generation | |
/// - Parameters: | |
/// - count: The number of random numbers to generate | |
/// - range: The range of values to generate | |
/// | |
/// Example: | |
/// | |
/// ```swift | |
/// let tenVals: [CGFloat] = RandomArray(count: 10, range: -10 ... 10) | |
/// ``` | |
/// | |
/// Note that the `[CGFloat]` in the definition is required, as explicit generic function instantiation isn't supported | |
/// in Swift. | |
func RandomArray<T>(count: UInt, range: ClosedRange<T>) -> Array<T> where T: BinaryFloatingPoint, T.RawSignificand : FixedWidthInteger { | |
return (0 ..< count).map { _ in T.random(in: range) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment