@@ -372,41 +372,75 @@ extension MutableCollection where Self : BidirectionalCollection {
372372//===----------------------------------------------------------------------===//
373373
374374extension Sequence {
375- /// Returns the elements of the sequence, shuffled.
375+ /// Returns the elements of the sequence, shuffled using the given generator
376+ /// as a source for randomness.
377+ ///
378+ /// You use this method to randomize the elements of a sequence when you
379+ /// are using a custom random number generator. For example, you can shuffle
380+ /// the numbers between `0` and `9` by calling the `shuffled(using:)` method
381+ /// on that range:
382+ ///
383+ /// let numbers = 0...9
384+ /// let shuffledNumbers = numbers.shuffled(using: &myGenerator)
385+ /// // shuffledNumbers == [8, 9, 4, 3, 2, 6, 7, 0, 5, 1]
376386 ///
377387 /// - Parameter generator: The random number generator to use when shuffling
378388 /// the sequence.
379- /// - Returns: A shuffled array of this sequence's elements.
389+ /// - Returns: An array of this sequence's elements in a shuffled order.
390+ ///
391+ /// - Complexity: O(*n*)
380392 @inlinable
381393 public func shuffled< T: RandomNumberGenerator > (
382- using generator: T
394+ using generator: inout T
383395 ) -> [ Element ] {
384396 var result = ContiguousArray ( self )
385- result. shuffle ( using: generator)
397+ result. shuffle ( using: & generator)
386398 return Array ( result)
387399 }
388400
389401 /// Returns the elements of the sequence, shuffled.
390402 ///
403+ /// For example, you can shuffle the numbers between `0` and `9` by calling
404+ /// the `shuffled()` method on that range:
405+ ///
406+ /// let numbers = 0...9
407+ /// let shuffledNumbers = numbers.shuffled()
408+ /// // shuffledNumbers == [1, 7, 6, 2, 8, 9, 4, 3, 5, 0]
409+ ///
410+ /// This method uses the default random generator, `Random.default`. The call
411+ /// to `numbers.shuffled()` above is equivalent to calling
412+ /// `numbers.shuffled(using: &Random.default)`.
413+ ///
391414 /// - Parameter generator: The random number generator to use when shuffling
392415 /// the sequence.
393416 /// - Returns: A shuffled array of this sequence's elements.
394417 ///
395- /// This uses the standard library's default random number generator.
418+ /// - Complexity: O(*n*)
396419 @inlinable
397420 public func shuffled( ) -> [ Element ] {
398- return shuffled ( using: Random . default)
421+ return shuffled ( using: & Random. default)
399422 }
400423}
401424
402425extension MutableCollection {
403- /// Shuffles the collection in place.
426+ /// Shuffles the collection in place, using the given generator as a source
427+ /// for randomness.
428+ ///
429+ /// You use this method to randomize the elements of a collection when you
430+ /// are using a custom random number generator. For example, you can use the
431+ /// `shuffle(using:)` method to randomly reorder the elements of an array.
432+ ///
433+ /// var names = ["Alejandro", "Camila", "Diego", "Luciana", "Luis", "Sofía"]
434+ /// names.shuffle(using: &myGenerator)
435+ /// // names == ["Sofía", "Alejandro", "Camila", "Luis", "Diego", "Luciana"]
404436 ///
405437 /// - Parameter generator: The random number generator to use when shuffling
406438 /// the collection.
439+ ///
440+ /// - Complexity: O(*n*)
407441 @inlinable
408442 public mutating func shuffle< T: RandomNumberGenerator > (
409- using generator: T
443+ using generator: inout T
410444 ) {
411445 guard count > 1 else { return }
412446 var amount = count
@@ -424,13 +458,21 @@ extension MutableCollection {
424458
425459 /// Shuffles the collection in place.
426460 ///
427- /// - Parameter generator: The random number generator to use when shuffling
428- /// the collection.
461+ /// Use the `shuffle()` method to randomly reorder the elements of an
462+ /// array.
463+ ///
464+ /// var names = ["Alejandro", "Camila", "Diego", "Luciana", "Luis", "Sofía"]
465+ /// names.shuffle(using: myGenerator)
466+ /// // names == ["Luis", "Camila", "Luciana", "Sofía", "Alejandro", "Diego"]
429467 ///
430- /// This uses the standard library's default random number generator.
468+ /// This method uses the default random generator, `Random.default`. The call
469+ /// to `names.shuffle()` above is equivalent to calling
470+ /// `names.shuffle(using: &Random.default)`.
471+ ///
472+ /// - Complexity: O(*n*)
431473 @inlinable
432474 public mutating func shuffle( ) {
433- shuffle ( using: Random . default)
475+ shuffle ( using: & Random. default)
434476 }
435477}
436478
0 commit comments