使arc4random更随机吗?

时间:2019-01-01 02:33:52

标签: swift arc4random

我正在尝试使随机问题出现在测验中。现在,我正在使用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
    }
}

2 个答案:

答案 0 :(得分:0)

首先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]()

以及通过用所有问题索引填充

来重置数组的函数
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进行比较。

旁注:由于DecrescendoDiminuendo实际上是同义词,因此在逐渐变得更柔和问题

中,最好使用Ritardando之类的东西。

答案 1 :(得分:-1)

请参见下面的代码

我在xib文件中使用了用于问题的标签和用于回答的4个按钮以及下一个和结果标签。逻辑的编写方式是,数组中的所有16个问题都被一个接一个地随机调用,并在回答后从数组中删除。所以没有机会重复。

screenshot     导入UIKit

struct Quiz {
    let Question: String
    let Answers: [String]
    let CorrectAns : Int
}

class HomeViewController: UIViewController {


@IBOutlet weak var lbl_Question: UILabel!
@IBOutlet weak var btn_Ans1: UIButton!
@IBOutlet weak var btn_Ans2: UIButton!
@IBOutlet weak var btn_Ans3: UIButton!
@IBOutlet weak var btn_Ans4: UIButton!
@IBOutlet weak var lbl_Result: UILabel!
 @IBOutlet weak var btn_Next: UIButton!


var quizArr = [
        [
            Quiz(Question: "1. What is the tail end of a piece of music?", Answers: ["Coda", "Da Capo", "Forte", "Largo"], CorrectAns: 1),
            Quiz(Question: "2. What is the tail end of a piece of music?", Answers: ["Coda", "Da Capo", "Forte", "Largo"], CorrectAns: 3),
            Quiz(Question: "3. What is the tail end of a piece of music?", Answers: ["Coda", "Da Capo", "Forte", "Largo"], CorrectAns: 4),
            Quiz(Question: "4. What is the tail end of a piece of music?", Answers: ["Coda", "Da Capo", "Forte", "Largo"], CorrectAns: 2)
        ],
        [
            Quiz(Question: "5. Another name for Majestically?", Answers: ["Lunga", "Dolce", "Maestoso", "Molto"], CorrectAns: 3),
            Quiz(Question: "6. Another name for Majestically?", Answers: ["Lunga", "Dolce", "Maestoso", "Molto"], CorrectAns: 4),
            Quiz(Question: "7. Another name for Majestically?", Answers: ["Lunga", "Dolce", "Maestoso", "Molto"], CorrectAns: 2),
            Quiz(Question: "8. Another name for Majestically?", Answers: ["Lunga", "Dolce", "Maestoso", "Molto"], CorrectAns: 1)
        ],
        [
            Quiz(Question: "9. Another name for Time/Speed?", Answers: ["Largo", "Soli", "Tenuto", "Tempo"], CorrectAns: 4),
            Quiz(Question: "10. Another name for Time/Speed?", Answers: ["Largo", "Soli", "Tenuto", "Tempo"], CorrectAns: 3),
            Quiz(Question: "11. Another name for Time/Speed?", Answers: ["Largo", "Soli", "Tenuto", "Tempo"], CorrectAns: 1),
            Quiz(Question: "12. Another name for Time/Speed?", Answers: ["Largo", "Soli", "Tenuto", "Tempo"], CorrectAns: 2)
        ],
        [
            Quiz(Question: "13. Another name for Softly", Answers: ["Piano", "Forte", "Segno", "Tacet"], CorrectAns: 3),
            Quiz(Question: "14. Another name for Softly", Answers: ["Piano", "Forte", "Segno", "Tacet"], CorrectAns: 4),
            Quiz(Question: "15. Another name for Softly", Answers: ["Piano", "Forte", "Segno", "Tacet"], CorrectAns: 1),
            Quiz(Question: "16. Another name for Softly", Answers: ["Piano", "Forte", "Segno", "Tacet"], CorrectAns: 2),
        ]
    ]

var randomInt = 0
var innerRandomInt = 0

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

   generateQuestion()
    btn_Next.isHidden = true
    btn_Next.addTarget(self, action: #selector(ClickedNext), for: .touchUpInside)
    btn_Ans1.tag = 1
    btn_Ans1.addTarget(self, action: #selector(checkAnswer), for: .touchUpInside)
    btn_Ans2.tag = 2
    btn_Ans2.addTarget(self, action: #selector(checkAnswer), for: .touchUpInside)
    btn_Ans3.tag = 3
    btn_Ans3.addTarget(self, action: #selector(checkAnswer), for: .touchUpInside)
    btn_Ans4.tag = 4
    btn_Ans4.addTarget(self, action: #selector(checkAnswer), for: .touchUpInside)
}

@objc func ClickedNext(){
    generateQuestion()
    btn_Next.isHidden = true
}

func generateQuestion(){
    let mainCount = quizArr.count
    if mainCount>0{
        randomInt = Int.random(in: 0..<mainCount)
        let innerCount = quizArr[randomInt].count
        if innerCount>0{
            innerRandomInt =  Int.random(in: 0..<innerCount)

        }else{
            generateQuestion()
            return
        }
        //print("Inner Random Number - \(innerRandomInt)")

        lbl_Question.text = quizArr[randomInt][innerRandomInt].Question
        btn_Ans1.setTitle(quizArr[randomInt][innerRandomInt].Answers[0], for: .normal)
        btn_Ans2.setTitle(quizArr[randomInt][innerRandomInt].Answers[1], for: .normal)
        btn_Ans3.setTitle(quizArr[randomInt][innerRandomInt].Answers[2], for: .normal)
        btn_Ans4.setTitle(quizArr[randomInt][innerRandomInt].Answers[3], for: .normal)

        lbl_Result.text = ""
    }else{
        lbl_Question.isHidden = true
        btn_Ans1.isHidden = true
        btn_Ans2.isHidden = true
        btn_Ans3.isHidden = true
        btn_Ans4.isHidden = true
        btn_Next.isHidden = true
        lbl_Result.text = "Quiz Over"
        print("Quiz Over")
        return
    }

}

@objc func checkAnswer(sender:UIButton){
    //print(sender.tag)
    let CorrectAns = quizArr[randomInt][innerRandomInt].CorrectAns
    if sender.tag == CorrectAns{
        lbl_Result.text = "Correct Answer"
    }else{
        lbl_Result.text = "Wrong Answer"
    }
    quizArr[randomInt].remove(at: innerRandomInt)
    btn_Next.isHidden = false
    for i in 0..<quizArr.count{
        if quizArr[i].count == 0{
            quizArr.remove(at: i)
            break
        }
    }

}

}