Type Method 型メソッド

random(in:)

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

Declaration 宣言

static func random(in range: Range<Self>) -> Self
Available when RawSignificand conforms to FixedWidthInteger. RawSignificandFixedWidthIntegerに準拠する時に利用可能です。

Parameters パラメータ

range

The range in which to create a random value. range must be finite and non-empty. その中においてある無作為な値が作成される範囲。rangeは有限でそして空でない必要があります。

Return Value 戻り値

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

Discussion 解説

Use this method to generate a floating-point value within a specific range. This example creates three new values in the range 10.0 ..< 20.0. このメソッドを使って、特定の範囲の内のある浮動小数点値を作成してください。この例は、3つの新しい値を範囲10.0 ..< 20.0において作成します。


for _ in 1...3 {
    print(Double.random(in: 10.0 ..< 20.0))
}
// Prints "18.1900709259179"
// Prints "14.2286325689993"
// Prints "13.1485686260762"

The random() 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. random()静的メソッドは無作為の値をrangeの中の連続一様分布から選びます、それからその値をその型で表現可能な最も近い値に変換します。rangeの大きさと幅に依存して、いくつかの具体的な値は他のものよりずっと頻繁に表現されるかもしれません。

This method is equivalent to calling random(in:using:), passing in the system’s default random generator. このメソッドは、random(in:using:)を、システムの持つ省略時の無作為生成子を渡して呼び出すことに相当します。

See Also 参照

Cerating a Random Value 無作為な値を作成する