【问题标题】:Make arc4random, more random?使 arc4random,更随机?
【发布时间】:2019-01-01 02:33:52
【问题描述】:

我正在尝试让随机问题出现在我的测验中。现在我正在使用arc4random() 为switch 语句提取一个随机数,这就是按钮和标签上显示的内容,但它喜欢重复一些问题。我怎样才能阻止这种情况发生或减少它的重复?

是的,我知道我经常重复代码,但目前我知道该怎么做!

func RandomQuestions() {
    var RandomNumber = arc4random() % 10
    var RandomQuestion = arc4random() % 4
    RandomQuestion += 1
    RandomNumber += 1
    Next.isHidden = true

    switch(RandomNumber) {

    case 1:
        if RandomQuestion == UInt32(1) {
            questionLabel.text = "What is the tail end of a piece of music?"
            button1.setTitle("Coda", for: UIControl.State.normal)
            button2.setTitle("Da Capo", for: UIControl.State.normal)
            button3.setTitle("Forte", for: UIControl.State.normal)
            button4.setTitle("Largo", for: UIControl.State.normal)
            CorrectAnswer = "1"
        } else if RandomQuestion == UInt32(2) {
            questionLabel.text = "What is the tail end of a piece of music?"
            button1.setTitle("Da Capo", for: UIControl.State.normal)
            button2.setTitle("Largo", for: UIControl.State.normal)
            button3.setTitle("Coda", for: UIControl.State.normal)
            button4.setTitle("Forte", for: UIControl.State.normal)
            CorrectAnswer = "3"
        } else if RandomQuestion == UInt32(3) {
            questionLabel.text = "What is the tail end of a piece of music?"
            button1.setTitle("Largo", for: UIControl.State.normal)
            button2.setTitle("Da Capo", for: UIControl.State.normal)
            button3.setTitle("Forte", for: UIControl.State.normal)
            button4.setTitle("Coda", for: UIControl.State.normal)
            CorrectAnswer = "4"
        } else {
            questionLabel.text = "What is the tail end of a piece of music?"
            button1.setTitle("Largo", for: UIControl.State.normal)
            button2.setTitle("Coda", for: UIControl.State.normal)
            button3.setTitle("Forte", for: UIControl.State.normal)
            button4.setTitle("Da Capo", for: UIControl.State.normal)
            CorrectAnswer = "2"
        }
        break
    case 2:
        if RandomQuestion == UInt32(1) {
            questionLabel.text = "Another name for Majestically?"
            button1.setTitle("Lunga", for: UIControl.State.normal)
            button2.setTitle("Dolce", for: UIControl.State.normal)
            button3.setTitle("Maestoso", for: UIControl.State.normal)
            button4.setTitle("Molto", for: UIControl.State.normal)
            CorrectAnswer = "3"
        } else if RandomQuestion == UInt32(2) {
            questionLabel.text = "Another name for Majestically?"
            button1.setTitle("Dolce", for: UIControl.State.normal)
            button2.setTitle("Lunga", for: UIControl.State.normal)
            button3.setTitle("Molto", for: UIControl.State.normal)
            button4.setTitle("Maestoso", for: UIControl.State.normal)
            CorrectAnswer = "4"
        } else if RandomQuestion == UInt32(3) {
            questionLabel.text = "Another name for Majestically?"
            button1.setTitle("Molto", for: UIControl.State.normal)
            button2.setTitle("Maestoso", for: UIControl.State.normal)
            button3.setTitle("Lunga", for: UIControl.State.normal)
            button4.setTitle("Dolce", for: UIControl.State.normal)
            CorrectAnswer = "2"
        } else {
            questionLabel.text = "Another name for Majestically?"
            button1.setTitle("Maestoso", for: UIControl.State.normal)
            button2.setTitle("Dolce", for: UIControl.State.normal)
            button3.setTitle("Lunga", for: UIControl.State.normal)
            button4.setTitle("Molto", for: UIControl.State.normal)
            CorrectAnswer = "1"
        }
        break
    case 3:
        if RandomQuestion == UInt32(1) {
            questionLabel.text = "Another name for Time/Speed?"
            button1.setTitle("Largo", for: UIControl.State.normal)
            button2.setTitle("Soli", for: UIControl.State.normal)
            button3.setTitle("Tenuto", for: UIControl.State.normal)
            button4.setTitle("Tempo", for: UIControl.State.normal)
            CorrectAnswer = "4"
        } else if RandomQuestion == UInt32(2) {
            questionLabel.text = "Another name for Time/Speed?"
            button1.setTitle("Soli", for: UIControl.State.normal)
            button2.setTitle("Tenuto", for: UIControl.State.normal)
            button3.setTitle("Tempo", for: UIControl.State.normal)
            button4.setTitle("Largo", for: UIControl.State.normal)
            CorrectAnswer = "3"
        } else if RandomQuestion == UInt32(3) {
            questionLabel.text = "Another name for Time/Speed?"
            button1.setTitle("Tempo", for: UIControl.State.normal)
            button2.setTitle("Largo", for: UIControl.State.normal)
            button3.setTitle("Soli", for: UIControl.State.normal)
            button4.setTitle("Tenuto", for: UIControl.State.normal)
            CorrectAnswer = "1"
        } else {
            questionLabel.text = "Another name for Time/Speed?"
            button1.setTitle("Largo", for: UIControl.State.normal)
            button2.setTitle("Tempo", for: UIControl.State.normal)
            button3.setTitle("Tenuto", for: UIControl.State.normal)
            button4.setTitle("Soli", for: UIControl.State.normal)
            CorrectAnswer = "2"
        }
        break
    case 4:
        if RandomQuestion == UInt32(1) {
            questionLabel.text = "Another name for Softly"
            button1.setTitle("Piano", for: UIControl.State.normal)
            button2.setTitle("Forte", for: UIControl.State.normal)
            button3.setTitle("Segno", for: UIControl.State.normal)
            button4.setTitle("Tacet", for: UIControl.State.normal)
            CorrectAnswer = "1"
        } else if RandomQuestion == UInt32(2) {
            questionLabel.text = "Another name for Softly"
            button1.setTitle("Forte", for: UIControl.State.normal)
            button2.setTitle("Tacet", for: UIControl.State.normal)
            button3.setTitle("Piano", for: UIControl.State.normal)
            button4.setTitle("Segno", for: UIControl.State.normal)
            CorrectAnswer = "3"
        } else if RandomQuestion == UInt32(3) {
            questionLabel.text = "Another name for Softly"
            button1.setTitle("Tacet", for: UIControl.State.normal)
            button2.setTitle("Segno", for: UIControl.State.normal)
            button3.setTitle("Forte", for: UIControl.State.normal)
            button4.setTitle("Piano", for: UIControl.State.normal)
            CorrectAnswer = "4"
        } else {
            questionLabel.text = "Another name for Softly"
            button1.setTitle("Forte", for: UIControl.State.normal)
            button2.setTitle("Piano", for: UIControl.State.normal)
            button3.setTitle("Segno", for: UIControl.State.normal)
            button4.setTitle("Tacet", for: UIControl.State.normal)
            CorrectAnswer = "2"
        }
        break
    case 5:
        if RandomQuestion == UInt32(1) {
            questionLabel.text = "Another name for Really Fast?"
            button1.setTitle("Staccato", for: UIControl.State.normal)
            button2.setTitle("Fermata", for: UIControl.State.normal)
            button3.setTitle("Presto", for: UIControl.State.normal)
            button4.setTitle("Mezzo", for: UIControl.State.normal)
            CorrectAnswer = "3"
        } else if RandomQuestion == UInt32(2) {
            questionLabel.text = "Another name for Really Fast?"
            button1.setTitle("Fermata", for: UIControl.State.normal)
            button2.setTitle("Presto", for: UIControl.State.normal)
            button3.setTitle("Staccato", for: UIControl.State.normal)
            button4.setTitle("Mezzo", for: UIControl.State.normal)
            CorrectAnswer = "2"
        } else if RandomQuestion == UInt32(3) {
            questionLabel.text = "Another name for Really Fast?"
            button1.setTitle("Fermata", for: UIControl.State.normal)
            button2.setTitle("Mezzo", for: UIControl.State.normal)
            button3.setTitle("Staccato", for: UIControl.State.normal)
            button4.setTitle("Presto", for: UIControl.State.normal)
            CorrectAnswer = "4"
        } else {
            questionLabel.text = "Another name for Really Fast?"
            button1.setTitle("Presto", for: UIControl.State.normal)
            button2.setTitle("Mezzo", for: UIControl.State.normal)
            button3.setTitle("Staccato", for: UIControl.State.normal)
            button4.setTitle("Fermata", for: UIControl.State.normal)
            CorrectAnswer = "1"
        }
    case 6:
        if RandomQuestion == UInt(1) {
            questionLabel.text = "Another way to say From The Beginning?"
            button1.setTitle("Da Capo", for: UIControl.State.normal)
            button2.setTitle("Molto", for: UIControl.State.normal)
            button3.setTitle("Dal Segno", for: UIControl.State.normal)
            button4.setTitle("Al Fine", for: UIControl.State.normal)
            CorrectAnswer = "1"
        } else if RandomQuestion == UInt(2) {
            questionLabel.text = "Another way to say From The Beginning?"
            button1.setTitle("Al Fine", for: UIControl.State.normal)
            button2.setTitle("Da Capo", for: UIControl.State.normal)
            button3.setTitle("Molto", for: UIControl.State.normal)
            button4.setTitle("Dal Segno", for: UIControl.State.normal)
            CorrectAnswer = "2"
        } else if RandomQuestion == UInt(3) {
            questionLabel.text = "Another way to say From The Beginning?"
            button1.setTitle("Molto", for: UIControl.State.normal)
            button2.setTitle("Al Fine", for: UIControl.State.normal)
            button3.setTitle("Dal Segno", for: UIControl.State.normal)
            button4.setTitle("Da Capo", for: UIControl.State.normal)
            CorrectAnswer = "4"
        } else {
            questionLabel.text = "Another way to say From The Beginning?"
            button1.setTitle("Al Fine", for: UIControl.State.normal)
            button2.setTitle("Molto", for: UIControl.State.normal)
            button3.setTitle("Da Capo", for: UIControl.State.normal)
            button4.setTitle("Dal Segno", for: UIControl.State.normal)
            CorrectAnswer = "3"
        }
    case 7:
        if RandomQuestion == UInt32(1) {
            questionLabel.text = "In a singing style"
            button1.setTitle("Cantabile", for: UIControl.State.normal)
            button2.setTitle("Diminuendo", for: UIControl.State.normal)
            button3.setTitle("Animato", for: UIControl.State.normal)
            button4.setTitle("Chromatic", for: UIControl.State.normal)
            CorrectAnswer = "1"
        } else if RandomQuestion == UInt32(2) {
            questionLabel.text = "In a singing style"
            button1.setTitle("Chromatic", for: UIControl.State.normal)
            button2.setTitle("Diminuendo", for: UIControl.State.normal)
            button3.setTitle("Animato", for: UIControl.State.normal)
            button4.setTitle("Cantabile", for: UIControl.State.normal)
            CorrectAnswer = "4"
        } else if RandomQuestion == UInt32(3) {
            questionLabel.text = "In a singing style"
            button1.setTitle("Diminuendo", for: UIControl.State.normal)
            button2.setTitle("Chromatic", for: UIControl.State.normal)
            button3.setTitle("Cantabile", for: UIControl.State.normal)
            button4.setTitle("Animato", for: UIControl.State.normal)
            CorrectAnswer = "3"
        } else {
            questionLabel.text = "In a singing style"
            button1.setTitle("Animato", for: UIControl.State.normal)
            button2.setTitle("Cantabile", for: UIControl.State.normal)
            button3.setTitle("Chromatic", for: UIControl.State.normal)
            button4.setTitle("Diminuendo", for: UIControl.State.normal)
            CorrectAnswer = "2"
        }
    case 8:
        if RandomQuestion == UInt32(1) {
            questionLabel.text = "Which also means From The Sign?"
            button1.setTitle("Dal Segno", for: UIControl.State.normal)
            button2.setTitle("Da Capo", for: UIControl.State.normal)
            button3.setTitle("Grave", for: UIControl.State.normal)
            button4.setTitle("Lento", for: UIControl.State.normal)
            CorrectAnswer = "1"
        } else if RandomQuestion == UInt32(2) {
            questionLabel.text = "Which also means From The Sign?"
            button1.setTitle("Lento", for: UIControl.State.normal)
            button2.setTitle("Dal Segno", for: UIControl.State.normal)
            button3.setTitle("Da Capo", for: UIControl.State.normal)
            button4.setTitle("Grave", for: UIControl.State.normal)
            CorrectAnswer = "2"
        } else if RandomQuestion == UInt32(3) {
            questionLabel.text = "Which also means From The Sign?"
            button1.setTitle("Lento", for: UIControl.State.normal)
            button2.setTitle("Grave", for: UIControl.State.normal)
            button3.setTitle("Da Capo", for: UIControl.State.normal)
            button4.setTitle("Dal Segno", for: UIControl.State.normal)
            CorrectAnswer = "4"
        } else {
            questionLabel.text = "Which also means From The Sign?"
            button1.setTitle("Da Capo", for: UIControl.State.normal)
            button2.setTitle("Lento ", for: UIControl.State.normal)
            button3.setTitle("Dal Segno", for: UIControl.State.normal)
            button4.setTitle("Grave", for: UIControl.State.normal)
            CorrectAnswer = "3"
        }
    case 9:
        if RandomQuestion == UInt(1) {
            questionLabel.text = "Which also means The Same As?"
            button1.setTitle("Simile", for: UIControl.State.normal)
            button2.setTitle("Rubato", for: UIControl.State.normal)
            button3.setTitle("Non Troppo", for: UIControl.State.normal)
            button4.setTitle("Mosso", for: UIControl.State.normal)
            CorrectAnswer = "1"
        } else if RandomQuestion == UInt32(2) {
            questionLabel.text = "Which also means The Same As?"
            button1.setTitle("Mosso", for: UIControl.State.normal)
            button2.setTitle("Non Troppo", for: UIControl.State.normal)
            button3.setTitle("Rubato", for: UIControl.State.normal)
            button4.setTitle("Simile", for: UIControl.State.normal)
            CorrectAnswer = "4"
        } else if RandomQuestion == UInt32(3) {
            questionLabel.text = "Which also means The Same As?"
            button1.setTitle("Rubato", for: UIControl.State.normal)
            button2.setTitle("Simile", for: UIControl.State.normal)
            button3.setTitle("Non Troppo", for: UIControl.State.normal)
            button4.setTitle("Mosso", for: UIControl.State.normal)
            CorrectAnswer = "2"
        } else {
            questionLabel.text = "Which also means The Same As?"
            button1.setTitle("Rubato", for: UIControl.State.normal)
            button2.setTitle("Mosso", for: UIControl.State.normal)
            button3.setTitle("Simile", for: UIControl.State.normal)
            button4.setTitle("Non Troppo", for: UIControl.State.normal)
            CorrectAnswer = "3"
        }
    case 10:
        if RandomQuestion == UInt32(1) {
            questionLabel.text = "Which also means Gradually Get Louder?"
            button1.setTitle("Crescendo", for: UIControl.State.normal)
            button2.setTitle("Decrescendo", for: UIControl.State.normal)
            button3.setTitle("Diminuendo", for: UIControl.State.normal)
            button4.setTitle("Mezzo Forte", for: UIControl.State.normal)
            CorrectAnswer = "1"
        } else if RandomQuestion == UInt32(2) {
            questionLabel.text = "Which also means Gradually Get Louder?"
            button1.setTitle("Diminuendo", for: UIControl.State.normal)
            button2.setTitle("Mezzo Forte", for: UIControl.State.normal)
            button3.setTitle("Crescendo", for: UIControl.State.normal)
            button4.setTitle("Decrescendo", for: UIControl.State.normal)
            CorrectAnswer = "3"
        } else if RandomQuestion == UInt32(3) {
            questionLabel.text = "Which also means Gradually Get Louder?"
            button1.setTitle("Diminuendo", for: UIControl.State.normal)
            button2.setTitle("Crescendo", for: UIControl.State.normal)
            button3.setTitle("Mezzo Forte", for: UIControl.State.normal)
            button4.setTitle("Decrescendo", for: UIControl.State.normal)
            CorrectAnswer = "2"
        } else {
            questionLabel.text = "Which also means Gradually Get Louder?"
            button1.setTitle("Diminuendo", for: UIControl.State.normal)
            button2.setTitle("Decrescendo", for: UIControl.State.normal)
            button3.setTitle("Mezzo Forte", for: UIControl.State.normal)
            button4.setTitle("Crescendo", for: UIControl.State.normal)
            CorrectAnswer = "4"
        }
    case 11:
        if RandomQuestion == UInt32(1) {
            questionLabel.text = "Which also means Gradually Get Softer?"
            button1.setTitle("Diminuendo", for: UIControl.State.normal)
            button2.setTitle("Decrescendo", for: UIControl.State.normal)
            button3.setTitle("Mezzo Forte", for: UIControl.State.normal)
            button4.setTitle("Crescendo", for: UIControl.State.normal)
            CorrectAnswer = "2"
        } else if RandomQuestion == UInt32(2) {
            questionLabel.text = "Which also means Gradually Get Softer?"
            button1.setTitle("Diminuendo", for: UIControl.State.normal)
            button2.setTitle("Mezzo Forte", for: UIControl.State.normal)
            button3.setTitle("Decrescendo", for: UIControl.State.normal)
            button4.setTitle("Crescendo", for: UIControl.State.normal)
            CorrectAnswer = "3"
        } else if RandomQuestion == UInt32(3) {
            questionLabel.text = "Which also means Gradually Get Softer?"
            button1.setTitle("Diminuendo", for: UIControl.State.normal)
            button2.setTitle("Mezzo Forte", for: UIControl.State.normal)
            button3.setTitle("Crescendo", for: UIControl.State.normal)
            button4.setTitle("Decrescendo", for: UIControl.State.normal)
            CorrectAnswer = "4"
        } else {
            questionLabel.text = "Which also means Gradually Get Louder?"
            button1.setTitle("Decrescendo", for: UIControl.State.normal)
            button2.setTitle("Crescendo", for: UIControl.State.normal)
            button3.setTitle("Diminuendo", for: UIControl.State.normal)
            button4.setTitle("Mezzo Forte", for: UIControl.State.normal)
            CorrectAnswer = "1"
        }
    default:

        break
    }
}

【问题讨论】:

  • 你的意思是它重复了很多问题?像randomNumber一样频繁吗?
  • 重复是随机数的生活事实。它们只是发生了。如果您希望选择的数字分布在您可能的问题中而不重复,那么您实际上并不想要随机性。考虑将您的问题放入一个数组中,随机选择一个,然后从数组中删除该问题并在您的应用程序中使用它。下次您选择一个随机问题时,您知道您从未使用的问题中进行选择。重复。
  • @Magnas 这是个好主意。我得试试。

标签: swift arc4random


【解决方案1】:

首先arc4random() 在 Swift 4.2+ 中已经过时了。

random(in 类方法适用于所有数值类型,shuffled() 和 randomElement() 用于从数组中获取随机元素。

要从数组中获取唯一的随机元素,您可以使用一个简单的函数,该函数选择一个随机元素,然后从数组中删除该项目。

假设你有 10 个问题

let numberOfQuestions = 10

创建索引数组

var indexArray = [Int](0..<numberOfQuestions)

并使用此功能

func uniqueRandomElement(from array: inout [Int]) -> Int? {
    guard let random = array.randomElement() else { return nil }
    let index = array.firstIndex(of: random)!
    array.remove(at: index)
    return random
}

let index = uniqueRandomElement(from: &indexArray)

它返回随机索引或nil(如果已询问所有问题)。


如果你将问题组织在一个结构中,代码可以大大减少

struct Question {
    let question : String
    let answers : [String]
    let correctAnswer : String
}

let questions = [Question(question: "What is the tail end of a piece of music?", answers: ["Coda", "Da Capo", "Forte", "Largo"], correctAnswer: "Coda"),
                 ...
                 ...
                 Question(question: "Which also means Gradually Get Softer?", answers: ["Ritardando", "Decrescendo", "Mezzo Forte", "Crescendo"], correctAnswer: "Decrescendo")]

为索引声明一个空数组

var indexArray = [Int]()

以及一个通过用all问题索引填充数组来重置数组的函数

func resetQuestions() {
    indexArray = [Int](0..<questions.count)
}

randomQuestion 使用uniqueRandomElement 函数获取新索引,将answers 数组打乱,并将值分配给按钮。它返回当前问题

func randomQuestion() -> Question? {
    guard let questionIndex = uniqueRandomElement(from: &indexArray) else {
        Next.isHidden = true
        return nil
    }

    let currentQuestion = questions[questionIndex]
    let shuffledAnswers = currentQuestion.answers.shuffled()
    button1.setTitle(shuffledAnswers[0], for: .normal)
    button2.setTitle(shuffledAnswers[1], for: .normal)
    button3.setTitle(shuffledAnswers[2], for: .normal)
    button4.setTitle(shuffledAnswers[3], for: .normal)
    return currentQuestion
}

现在将按下按钮的标题与currentQuestion.correctAnswer 进行比较。

旁注:因为Decrescendo 和Diminuendo 实际上是同义词,所以最好在逐渐变软 问题中使用Ritardando 之类的东西?

【讨论】:

    猜你喜欢
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多