Type Method 型メソッド

random(in:)

Returns a random value within the specified range. 指定された範囲内のある無作為の値を返します。

Declaration 宣言

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

Parameters パラメータ

range

The range in which to create a random value. その中においてある無作為な値が作成される範囲。

Return Value 戻り値

A random value within the bounds of range. rangeの境界内のある無作為な値。

Discussion 解説

Use this method to generate an integer within a specific range. This example creates three new values in the range 1...100. このメソッドを使って、特定の範囲の内のある整数を作成してください。この例は、3つの新しい値を範囲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. このメソッドは、random(in:using:)を、システムの持つ省略時の無作為生成子を渡して呼び出すことに相当します。