我在結構中使用GKRandomSource
在視圖中返回一個隨機勵志名言。有沒有辦法返回這個隨機數並省略之前的輸入?這樣用戶不會連續兩次收到相同的報價。使用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]
}
最好有每次數組的副本和你採取隨機索引,從數組中刪除它,然後隨機從0到新的陣列大小 – Fonix