jeudi 16 juillet 2015

Reoccurring random numbers

Im building a multiple choice quiz game and have 4 random answer choices and 1 correct answer choice. For every other question the random answer choices get repeated like choice 1 and choice 3 are both 4. I want that to not happen. Here is my code.

var correctans = num1.text!.toInt()! + num2.text!.toInt()!
    var randomans1 = (arc4random()%8) + 1
    var randomans2 = (arc4random()%7) + 1
    var randomans3 = (arc4random()%6) + 1
    var randomans4 = (arc4random()%5) + 1


    switch (randomplace) {

        case 1:

            btn3.setTitle("\(correctans)", forState: UIControlState.Normal)
            btn4.setTitle("\(randomans1)", forState: UIControlState.Normal)
            btn2.setTitle("\(randomans3)", forState: UIControlState.Normal)
            btn1.setTitle("\(randomans4)", forState: UIControlState.Normal)

            break

and then it goes on for case 2,3,4. The highest sum of the two numbers can be 8




Aucun commentaire:

Enregistrer un commentaire