
我的主力博客:半亩方塘
The function below returns a random number between 0 and the given argument:
import Foundationfunc randomFromZeroTo(number: Int) -> Int { return Int(arc4random_uniform(UInt32(number)))} Use it to write a function that shuffles the elements of an array in random order. This is the signature of the function:
func randomArray(array: [Int]) -> [Int]
The answer is below:
func randomArray(array: [Int]) -> [Int] { var newArray = array for index in 0..<array.count { let randomIndex = randomFromZeroTo(array.count) let value = newArray[index] newArray[index] = newArray[randomIndex] newArray[randomIndex] = value } return newArray} 总结 以上是内存溢出为你收集整理的Swift 笔记(九)全部内容,希望文章能够帮你解决Swift 笔记(九)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)