0

I'm trying to add a random number (1...8) into an array. I want to do this 16 times. The clue is, every number should only be in there twice. I got it working so far, but I don't think this is a clean way to do it. And the way I did it, it is not scaleable. If i wanted it to be, let's say for example 24 elements, and all of them 3 times, I'd have to rewrite all the code. Do You have an idea how to fix that? Thanks in advance!

var Numbers: [Int] = []
var randomNumber: Int = 0
let size: Int = 16

var index1 = 0
var index2 = 0
var index3 = 0
var index4 = 0
var index5 = 0
var index6 = 0
var index7 = 0
var index8 = 0

// Methods / Functions

func createRandomNumber() {

    randomNumber = Int(arc4random() % 8 + 1)
}

func checkTwins() {

    switch randomNumber {
    case 1:
        index1++
        if index1 <= 2 {
            Numbers.append(randomNumber)
        }
        else {
            addRandomNumbertoArray()
        }
    case 2:
        index2++
        if index2 <= 2{
            Numbers.append(randomNumber)
        }
        else {
            addRandomNumbertoArray()
        }
    case 3:
        index3++
        if index3 <= 2 {
            Numbers.append(randomNumber)
        }
        else {
            addRandomNumbertoArray()
        }
    case 4:
        index4++
        if index4 <= 2 {
                Numbers.append(randomNumber)
        }
        else {
            addRandomNumbertoArray()
        }
    case 5:
        index5++
        if index5 <= 2 {
                Numbers.append(randomNumber)
        }
        else {
            addRandomNumbertoArray()
        }
    case 6:
        index6++
        if index6 <= 2 {
                Numbers.append(randomNumber)
        }
        else {
            addRandomNumbertoArray()
        }
    case 7:
        index7++
        if index7 <= 2 {
                Numbers.append(randomNumber)

        }
        else {
            addRandomNumbertoArray()
        }
    case 8:
        index8++
        if index8 <= 2 {
                Numbers.append(randomNumber)

        }
        else {
            addRandomNumbertoArray()
        }

    default: println("Generated Number out of Index")
    }
}

func addRandomNumbertoArray() {
    createRandomNumber()
    checkTwins()
}

func fillUpArray() {

    for _ in 1...size {
        addRandomNumbertoArray()
    }
}

// Calls

fillUpArray()
println(Numbers)

This was written in Swift Playground

smnk
  • 471
  • 1
  • 6
  • 25

1 Answers1

1

What you can do is: First fill an array containing each of the number 1, ..., 8 twice:

var array : [Int] = []
for i in 1 ... 8 {
    array.extend(Repeat(count: 2, repeatedValue: i))
}

and then shuffle the array randomly:

array.shuffle()

where shuffle() is a Fisher-Yates shuffle. A Swift implementation can be found in How do I shuffle an array in Swift?:

extension Array {
    mutating func shuffle() {
        for i in 0..<(count - 1) {
            let j = Int(arc4random_uniform(UInt32(count - i))) + i
            swap(&self[i], &self[j])
        }
    }
}

This scales without problems to larger arrays and more repeated elements.

Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382