Generic Instance Method 総称体インスタンスメソッド

shuffled(using:)

Returns the elements of the sequence, shuffled using the given generator as a source for randomness. シーケンスの要素を返します、与えられた生成子を無作為さの出典として使って混ぜ合わされます。

Declaration 宣言

func shuffled<T>(using generator: inout T) -> [Element] where T : RandomNumberGenerator

Parameters パラメータ

generator

The random number generator to use when shuffling the sequence. シーケンスをシャッフルするときに使う無作為数生成子。

Return Value 戻り値

An array of this sequence’s elements in a shuffled order. このシーケンスの持つ要素からなる配列、シャッフルされた順番で。

Discussion 解説

You use this method to randomize the elements of a sequence when you are using a custom random number generator. For example, you can shuffle the numbers between 0 and 9 by calling the shuffled(using:) method on that range: あなたはこのメソッドを使って、あなたがあつらえの無作為数生成子を使っている場合に、あるシーケンスの要素を無作為化します。例えば、あなたは09の間の数をシャッフルすることが、shuffled(using:)メソッドをその範囲上で呼び出すことによって可能です:


let numbers = 0...9
let shuffledNumbers = numbers.shuffled(using: &myGenerator)
// shuffledNumbers == [8, 9, 4, 3, 2, 6, 7, 0, 5, 1]

Complexity: O(n), where n is the length of the sequence. 計算量:O(n)、ここでnはシーケンスの長さです。

See Also 参照

Reordering an Array's Elements 配列の要素を再配列します