Skip to main content
added 4 characters in body
Source Link
Robert Dresler
  • 11.2k
  • 2
  • 28
  • 50

##Using shuffledShuffled 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 numbersUnique 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])") } 

##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] { 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])") } 

##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])") } 
added 10 characters in body
Source Link
Robert Dresler
  • 11.2k
  • 2
  • 28
  • 50

##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] { 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])") } 

##Using shuffled I suppose you just need to get 5 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] { 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])") } 

##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] { 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])") } 
added 63 characters in body
Source Link
Robert Dresler
  • 11.2k
  • 2
  • 28
  • 50

I##Using shuffled I suppose you just need to get 5 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 buttonPressedbuttonPressed 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]) } 

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

func generateNumbers(repetitions: Int, maxValue: Int) -> [Int] { 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 } 

soand 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])") } 

I suppose you just need to get 5 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]) } 

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

func generateNumbers(repetitions: Int, maxValue: Int) -> [Int] { 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 } 

so 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])") } 

##Using shuffled I suppose you just need to get 5 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] { 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])") } 
added 181 characters in body
Source Link
Robert Dresler
  • 11.2k
  • 2
  • 28
  • 50
Loading
added 181 characters in body
Source Link
Robert Dresler
  • 11.2k
  • 2
  • 28
  • 50
Loading
added 181 characters in body
Source Link
Robert Dresler
  • 11.2k
  • 2
  • 28
  • 50
Loading
added 181 characters in body
Source Link
Robert Dresler
  • 11.2k
  • 2
  • 28
  • 50
Loading
Source Link
Robert Dresler
  • 11.2k
  • 2
  • 28
  • 50
Loading