Type Method

random(in:)

Returns a random value within the specified range.

Declaration

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

Parameters

range

The range in which to create a random value.

Return Value

A random value within the bounds of range.

Discussion

Use this method to generate an integer within a specific range. This example creates three new values in the range 1...100.

for _ in 1...3 {
    print(Int.random(in: 1...100))
}
// Prints "53"
// Prints "64"
// Prints "5"

This method is equivalent to calling random(in:using:), passing in the system’s default random generator.

See Also

Creating a Random Integer

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

Returns a random value within the specified range.

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

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

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.