File tree Expand file tree Collapse file tree 1 file changed +4
-4
lines changed
Shuffle/Shuffle.playground Expand file tree Collapse file tree 1 file changed +4
-4
lines changed Original file line number Diff line number Diff line change 33import Foundation
44
55/* Returns a random integer between 0 and n-1. */
6- public func random( n: Int ) -> Int {
6+ public func random( _ n: Int ) -> Int {
77 return Int ( arc4random_uniform ( UInt32 ( n) ) )
88}
99
@@ -12,7 +12,7 @@ public func random(n: Int) -> Int {
1212/* Fisher-Yates / Knuth shuffle */
1313extension Array {
1414 public mutating func shuffle( ) {
15- for i in ( count - 1 ) . stride ( through: 1 , by: - 1 ) {
15+ for i in stride ( from : count - 1 , through: 1 , by: - 1 ) {
1616 let j = random ( i + 1 )
1717 if i != j {
1818 swap ( & self [ i] , & self [ j] )
@@ -29,8 +29,8 @@ list.shuffle()
2929
3030
3131/* Create a new array of numbers that is already shuffled. */
32- public func shuffledArray( n: Int ) -> [ Int ] {
33- var a = [ Int] ( count : n , repeatedValue : 0 )
32+ public func shuffledArray( _ n: Int ) -> [ Int ] {
33+ var a = [ Int] ( repeating : 0 , count : n )
3434 for i in 0 ..< n {
3535 let j = random ( i + 1 )
3636 if i != j {
You can’t perform that action at this time.
0 commit comments