Swift 笔记(九)

Swift 笔记(九),第1张

概述我的主力博客:半亩方塘 Randomizing an array The function below returns a random number between 0 and the given argument: import Foundationfunc randomFromZeroTo(number: Int) -> Int { return Int(arc4random_un

我的主力博客:半亩方塘


Randomizing an array

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 笔记(九)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1079284.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存