Generic Type Method

random(in:using:)

Returns a random value within the specified range, using the given generator as a source for randomness.

Declaration

static func random<T>(in range: Range<Int>, using generator: inout T) -> Int where T : RandomNumberGenerator

Parameters

range

The range in which to create a random value. range must not be empty.

generator

The random number generator to use when creating the new random value.

Return Value

A random value within the bounds of range.

Discussion

Use this method to generate an integer within a specific range when you are using a custom random number generator. This example creates three new values in the range 1..<100.

for _ in 1...3 {
    print(Int.random(in: 1..<100, using: &myGenerator))
}
// Prints "7"
// Prints "44"
// Prints "21"

See Also

Creating a Random Integer

static func random(in: Range<Int>) -> Int

Returns a random value within the specified range.

static func random(in: ClosedRange<Int>) -> Int

Returns a random value within the specified range.

static func random<T>(in: ClosedRange<Int>, using: inout T) -> Int

Returns a random value within the specified range, using the given generator as a source for randomness.