2

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])
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Darren Jay
  • 49
  • 4
  • 1
    Have a look [here](https://stackoverflow.com/a/53027138/2907715) – ielyamani Nov 13 '18 at 17:06
  • `var ballArray2 = ballArray; random1 = Int.random(in 0 ... ballArray2.count), view1 = UIImage(named: ballArray2[random1]); ballArray.remove(at: random1), random2 = Int.random(in: 0 ... ballArray2.count);` etc.? Not sure of the limit, might be ballArray2.count-1 – Larme Nov 13 '18 at 17:08

3 Answers3

7

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])")
}
Robert Dresler
  • 10,580
  • 2
  • 22
  • 40
  • I like this solution, another option is to have array property with last selcted values and one function wich can check if the number is already selected to get another one. – m1sh0 Nov 13 '18 at 17:15
  • [`shuffle()`](https://developer.apple.com/documentation/swift/array/2994753-shuffle) shuffles the collection in place. Use [`shuffled()`](https://developer.apple.com/documentation/swift/array/2994757-shuffled) instead. – nayem Nov 13 '18 at 17:27
  • Hi Robert, The First answer you gave me so far seems to work pretty well, am i right in assuming, I will not get duplicate numbers like two 1's or two 49's or anything else being displayed? – Darren Jay Nov 13 '18 at 18:58
  • Well You have earned yourself a Tick sir, and Thank you very much for your assistant. It goes towards my learning experience of Swift and programming in general. Thanks again – Darren Jay Nov 13 '18 at 19:03
0

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]
    }
}
impression7vx
  • 1,728
  • 1
  • 20
  • 50
0

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.

Viren Malhan
  • 115
  • 1
  • 5