使用GKRandomSource生成随机数

时间:2016-10-03 02:46:13

标签: ios swift random shuffle gameplay-kit

我在结构中使用GKRandomSource在视图中返回随机鼓舞人心的引用。有没有办法返回该随机数并省略之前的条目?这样,用户连续两次不会收到相同的报价。

let inspiration = [
    "You are looking rather nice today, as always.",
    "Hello gorgeous!",
    "You rock, don't ever change!",
    "Your hair is looking on fleek today!",
    "That smile.",
    "Somebody woke up on the right side of bed!"]

func getRandomInspiration() -> String {
    let randomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(inspiration.count)
    return inspiration[randomNumber]
}

1 个答案:

答案 0 :(得分:2)

要避免生成相同的引号,请跟踪名为struct的{​​{1}}属性中的最后一个。然后将最大随机数减少1,如果生成与lastQuote相同,请改用lastQuote

max