Random Numbers in Swift

Viewed 297

I would like to create random numbers to be displayed in 5 UIImage boxes on the front of the app. However, I would like those numbers to not always be in the same range. For instance, my code below shows 0 ... 9 for randomPoolBallIndex3. But instead, I would like it to show any number from 0 ... 49 but not duplicate the same number on the other randomPoolBallIndex's. So every time the button is pressed it will not display, let's say 1, 1, 34, 35 and 50, but instead each number will be different.

Is there a way to pull this off?

I broke the array down from 0 ... 49 for each randomPoolBallIndex's but now they will only display what I have set the ranges for and I am not entirely happy, while it has resolved the duplication problem.

Code Below:

let ballArray = ["poolball1","poolball2","poolball3","poolball4","poolball5","poolball6","poolball7","poolball8","poolball9","poolball10","poolball11","poolball12","poolball13","poolball14","poolball15","poolball16","poolball17","poolball18","poolball19","poolball20","poolball21","poolball22","poolball23","poolball24","poolball25","poolball26","poolball27","poolball28","poolball29","poolball30","poolball31","poolball32","poolball33","poolball34","poolball35","poolball36","poolball37","poolball38","poolball39","poolball40","poolball41","poolball42","poolball43","poolball44","poolball45","poolball46","poolball47","poolball48","poolball49","poolball50"]

var randomPoolBallIndex: Int = 0
var randomPoolBallIndex1: Int = 0
var randomPoolBallIndex2: Int = 0
var randomPoolBallIndex3: Int = 0
var randomPoolBallIndex4: Int = 0
var randomPoolBallIndex5: Int = 0

@IBOutlet weak var poolBallView1: UIImageView!
@IBOutlet weak var poolBallView2: UIImageView!
@IBOutlet weak var poolBallView3: UIImageView!
@IBOutlet weak var poolBallView4: UIImageView!
@IBOutlet weak var poolBallView5: UIImageView!

@IBAction func buttonPressed(_ sender: UIButton) {
    randomPoolBallIndex1 = Int.random(in: 20 ... 29)
    randomPoolBallIndex2 = Int.random(in: 40 ... 49)
    randomPoolBallIndex3 = Int.random(in: 0 ... 9)
    randomPoolBallIndex4 = Int.random(in: 30 ... 39)
    randomPoolBallIndex5 = Int.random(in: 10 ... 19)

    poolBallView1.image = UIImage(named: ballArray[randomPoolBallIndex1])
    poolBallView2.image = UIImage(named: ballArray[randomPoolBallIndex2])
    poolBallView3.image = UIImage(named: ballArray[randomPoolBallIndex3])
    poolBallView4.image = UIImage(named: ballArray[randomPoolBallIndex4])
    poolBallView5.image = UIImage(named: ballArray[randomPoolBallIndex5])
3 Answers

Using Shuffled

I suppose you just need to get 5 different random pool ball names from your ballArray. So you don't need to generate any random numbers. Just in buttonPressed create a constant from shuffled ballArray

let shuffledBallArray = ballArray.shuffled()

now just set images like this:

poolBallView1.image = UIImage(named: shuffledBallArray[0])
poolBallView2.image = UIImage(named: shuffledBallArray[1])
...

So your buttonPressed action should look like this:

@IBAction func buttonPressed(_ sender: UIButton) {

    let shuffledBallArray = ballArray.shuffled()

    poolBallView1.image = UIImage(named: shuffledBallArray[0])
    poolBallView2.image = UIImage(named: shuffledBallArray[1])
    poolBallView3.image = UIImage(named: shuffledBallArray[2])
    poolBallView4.image = UIImage(named: shuffledBallArray[3])
    poolBallView5.image = UIImage(named: shuffledBallArray[4])
}

Creating Unique Random Numbers

Alternatively you can create function which gives you 5 unique random numbers

func generateNumbers(repetitions: Int, maxValue: Int) -> [Int] {

    var numbers = [Int]()

    for _ in 1...repetitions {
        var n: Int
        repeat {
            n = Int.random(in: 1...maxValue)
        } while numbers.contains(n)
        numbers.append(n)
    }

    return numbers
}

and in buttonPressed just create constant for this array of random numbers and set images without saving any image names somewhere in ballArray with hardcoded 50 names

@IBAction func buttonPressed(_ sender: UIButton) {

    let randomNumbers = generateNumbers(repetitions: 5, maxValue: 50)

    poolBallView1.image = UIImage(named: "poolBall\(randomNumbers[0])")
    poolBallView2.image = UIImage(named: "poolBall\(randomNumbers[1])")
    poolBallView3.image = UIImage(named: "poolBall\(randomNumbers[2])")
    poolBallView4.image = UIImage(named: "poolBall\(randomNumbers[3])")
    poolBallView5.image = UIImage(named: "poolBall\(randomNumbers[4])")
}

Create an array. They allow for the storage of multitudes of data and you can reference all of them. Same for images

var randomPoolBallIndices:[Int]!
@IBOutlet weak var poolBallViews: [UIImageView]! //Look up how to make array from IBOutlets

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    randomPoolBallIndices = Array(repeating: 0, count: 5)
}

@IBAction func buttonPressed(_ sender: UIButton) {

    for index in randomPoolBallIndices.indices {
        let number = Int.random(in: 0 ... 49)
        while (randomPoolBallIndices.contains(number)) {
            number = Int.random(in: 0 ... 49)
        }
        randomPoolBallIndices[index] = number
        poolBallViews[index] = randomPoolBallIndices[index]
    }
}

Generate random number and insert it in Set. When Set count reaches 5, break the loop. That way you can avoid duplication. Then create array of random numbers from set.

Related