gpt4 book ai didi

ios - 如何获得像我的值一样随机化的 key ?

转载 作者:行者123 更新时间:2023-11-30 12:17:16 25 4
gpt4 key购买 nike

我正在为 iOS 测验应用程序编写代码,到目前为止,该代码包含一个字典,该字典以问题为键,以该问题的答案为值。我的代码随机化一组键(问题)并选择一个要随机显示的键。我还将值(答案)放入值数组中并将它们随机化,并确保选择问题数组的任何索引都选择答案数组的相同索引。

但是,当我运行模拟器时,问题标签文本保持不变,并且不会从要选择的问题数组中选择新索引作为新显示的问题,但另一方面,在我运行之后,答案数组会发生变化选择一个答案。

简单地说,问题标签文本始终保持不变,而按钮的标题(值数组)确实选择不同的索引。 如何才能使问题标签发生变化而不保持不变?

这是我的代码:

class QuestionDetails {
let QADictionary = [
"Who is Thor's brother?" : ["Atum", "Loki", "Red Norvell", "Kevin Masterson"],
"What is the name of Thor's hammer?" : ["Mjolinr", "Uru", "Stormbreaker", "Thundara"],
"Who is the father of Thor?" : ["Odin", "Sif", "Heimdall", "Balder"]
]

//get question list
func questionWithAnswers() {

let listQuestions = QuestionDetails()

var questionList = Array(listQuestions.QADictionary.keys)

//random question index
var rand = Int(arc4random_uniform(UInt32(questionList.count)))

let randAnswers = rand

//random answer index
var answerList = Array(listQuestions.QADictionary.values)

var choices = answerList[randAnswers]

//fetch questions from list
var question = questionList[rand]

questionLabel.text = question

//function for new question and button titles
rightAnswerBox = arc4random_uniform(4)+1

//create button
var button:UIButton = UIButton()
var x = 1

for index in 1...4
{
button = view.viewWithTag(index) as! UIButton

if (index == Int(rightAnswerBox))
{
button.setTitle(choices[0], for: .normal)
}
else {
button.setTitle(choices[x], for: .normal)
x += 1
}

randomImage()
}
}

//variables
var currentQuestion = 0
var rightAnswerBox:UInt32 = 0
var index = 0

//Question Label
@IBOutlet weak var questionLabel: UILabel!

//Answer Button
@IBAction func buttonAction(_ sender: AnyObject) {

if (sender.tag == Int(rightAnswerBox)) {

//removes asked question

print ("Correct!")
}
else {
wrongSeg()
print ("Wrong!")
}
}

override func viewDidAppear(_ animated: Bool) {
questionWithAnswers()
}
}

最佳答案

获取字典的。强制转换为数组。获取数组的计数。生成 0 到 count 之间的随机数(不包括在内)。将随机数转换为 Int。获取该索引处的数组元素。使用它作为您的 key 。

测试上述方法:

extension ClosedRange where Bound : FloatingPoint {
public func random() -> Bound {
let range = self.upperBound - self.lowerBound
let randomValue = (Bound(arc4random_uniform(UINT32_MAX)) / Bound(UINT32_MAX)) * range + self.lowerBound
return randomValue
}
}

let QADictionary = [
"Who is Thor's brother?" : ["Atum", "Loki", "Red Norvell", "Kevin Masterson"],
"What is the name of Thor's hammer?" : ["Mjolinr", "Uru", "Stormbreaker", "Thundara"],
"Who is the father of Thor?" : ["Odin", "Sif", "Heimdall", "Balder"]
]
func test () {
let questions = Array(QADictionary.keys)
let r = (0...Double(questions.count)).random()
let randomQuestion = questions[Int(r)]
print(randomQuestion)
}
(1...40).forEach {_ in test()}
/*
Who is Thor's brother?
What is the name of Thor's hammer?
What is the name of Thor's hammer?
Who is Thor's brother?
Who is the father of Thor?
Who is Thor's brother?
Who is Thor's brother?
What is the name of Thor's hammer?
Who is Thor's brother?
What is the name of Thor's hammer?
Who is Thor's brother?
Who is Thor's brother?
Who is the father of Thor?
What is the name of Thor's hammer?
Who is the father of Thor?
What is the name of Thor's hammer?
Who is the father of Thor?
What is the name of Thor's hammer?
Who is the father of Thor?
What is the name of Thor's hammer?
What is the name of Thor's hammer?
Who is Thor's brother?
What is the name of Thor's hammer?
Who is Thor's brother?
Who is Thor's brother?
What is the name of Thor's hammer?
Who is Thor's brother?
Who is Thor's brother?
Who is Thor's brother?
What is the name of Thor's hammer?
What is the name of Thor's hammer?
What is the name of Thor's hammer?
Who is the father of Thor?
Who is Thor's brother?
Who is Thor's brother?
Who is Thor's brother?
Who is Thor's brother?
Who is Thor's brother?
What is the name of Thor's hammer?
What is the name of Thor's hammer?

*/

关于ios - 如何获得像我的值一样随机化的 key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45249499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com