Static Methodswift 6.0.1Swift
random(in:using:)
Returns a random value within the specified range, using the given generator as a source for randomness.
static func random<T>(in range: ClosedRange<Self>, using generator: inout T) -> Self where T : RandomNumberGenerator
Parameters
Returns
A random value within the bounds of range
.
Use this method to generate a floating-point value within a specific range when you are using a custom random number generator. This example creates three new values in the range 10.0 ... 20.0
.
for _ in 1...3 {
print(Double.random(in: 10.0 ... 20.0, using: &myGenerator))
}
// Prints "18.1900709259179"
// Prints "14.2286325689993"
// Prints "13.1485686260762"
The random(in:using:)
static method chooses a random value from a continuous uniform distribution in range
, and then converts that value to the nearest representable value in this type. Depending on the size and span of range
, some concrete values may be represented more frequently than others.
Other members in extension
Type members
init<Source>(Source
) Creates a new value, rounded to the closest possible representation.
init?<Source>(exactly: Source
) Creates a new value, if the given integer can be represented exactly.
static func random(in: ClosedRange<Self>
) -> Self Returns a random value within the specified range.
static func random(in: Range<Self>
) -> Self Returns a random value within the specified range.
static func random<T>(in: Range<Self>, using: inout T
) -> Self Returns a random value within the specified range, using the given generator as a source for randomness.