iOS 编程 : Making an arc4_random not repeat itself using a do while loop

标签 ios swift arc4random

<分区>

在此代码中,第二行遍历数组并输出它接收到的内容及其随机数。但有时我会说同样的话两次,就像它会说“Straub”,然后是“Straub”,然后是“Rusher”之类的东西。我试过做一个“do while loop”,但我不知道如何在它不重复的地方设置它。顺便说一句,这是一种快速的编程语言。

let types = ["Alex", "Straub", "Rusher", "Graser"]

let type = types[Int(arc4random_uniform(UInt32(types.count)))]

println(type)

有什么问题可以在评论区留言

最佳答案

这避免了直接重复:

var lastIndex = -1
var index = -1

let types = ["Alex", "Straub", "Rusher", "Graser"]

do {
    index = Int(arc4random_uniform(UInt32(types.count)))
} while index == lastIndex

println(types[index])
lastIndex = index

关于iOS 编程 : Making an arc4_random not repeat itself using a do while loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26204633/

相关文章:

ios - Xamarin.Forms - 从 MVVMCross 4 迁移到 5 - MvxFormsApp 不存在?

swift - 模态呈现/堆叠 View Controller 的含义

ios - 在 foreach 中从 NSDictionary 获取值?

ios - 为什么我的 UIPickerView 仅在不在 super View 中时才会响应?

ios - swift 3 : Swipe to 'Flag' , 如何

ios - 随机数生成语法正确,但 CPU 使用率达到 100% 并且其他应该同时发生的事情没有发生

swift - 如何从 arc4random 中排除特定整数

ios - Objective-C中random()函数源码

ios - 体系结构 x86_64 iOS 的 undefined symbol

ios - 使用 AVFoundation 获取视频的持续时间