Random Numbers in Swift
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])
ios swift
add a comment |
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])
ios swift
1
Have a look here
– Carpsen90
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
add a comment |
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])
ios swift
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])
ios swift
ios swift
edited Nov 13 '18 at 17:03
rmaddy
242k27316380
242k27316380
asked Nov 13 '18 at 17:01
Darren JayDarren Jay
213
213
1
Have a look here
– Carpsen90
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
add a comment |
1
Have a look here
– Carpsen90
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
1
1
Have a look here
– Carpsen90
Nov 13 '18 at 17:06
Have a look here
– Carpsen90
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
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
add a comment |
3 Answers
3
active
oldest
votes
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])")
}
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()
shuffles the collection in place. Useshuffled()
instead.
– nayem
Nov 13 '18 at 17:27
@nayem thank you, you are right. I fixed it.
– Robert Dresler
Nov 13 '18 at 17:29
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
@DarrenJay yes, you are right! :-)
– Robert Dresler
Nov 13 '18 at 18:59
|
show 1 more comment
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]
}
}
add a comment |
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286075%2frandom-numbers-in-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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])")
}
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()
shuffles the collection in place. Useshuffled()
instead.
– nayem
Nov 13 '18 at 17:27
@nayem thank you, you are right. I fixed it.
– Robert Dresler
Nov 13 '18 at 17:29
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
@DarrenJay yes, you are right! :-)
– Robert Dresler
Nov 13 '18 at 18:59
|
show 1 more comment
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])")
}
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()
shuffles the collection in place. Useshuffled()
instead.
– nayem
Nov 13 '18 at 17:27
@nayem thank you, you are right. I fixed it.
– Robert Dresler
Nov 13 '18 at 17:29
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
@DarrenJay yes, you are right! :-)
– Robert Dresler
Nov 13 '18 at 18:59
|
show 1 more comment
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])")
}
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])")
}
edited Dec 23 '18 at 11:57
answered Nov 13 '18 at 17:08
Robert DreslerRobert Dresler
5,8701526
5,8701526
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()
shuffles the collection in place. Useshuffled()
instead.
– nayem
Nov 13 '18 at 17:27
@nayem thank you, you are right. I fixed it.
– Robert Dresler
Nov 13 '18 at 17:29
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
@DarrenJay yes, you are right! :-)
– Robert Dresler
Nov 13 '18 at 18:59
|
show 1 more comment
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()
shuffles the collection in place. Useshuffled()
instead.
– nayem
Nov 13 '18 at 17:27
@nayem thank you, you are right. I fixed it.
– Robert Dresler
Nov 13 '18 at 17:29
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
@DarrenJay yes, you are right! :-)
– Robert Dresler
Nov 13 '18 at 18:59
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
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()
shuffles the collection in place. Use shuffled()
instead.– nayem
Nov 13 '18 at 17:27
shuffle()
shuffles the collection in place. Use shuffled()
instead.– nayem
Nov 13 '18 at 17:27
@nayem thank you, you are right. I fixed it.
– Robert Dresler
Nov 13 '18 at 17:29
@nayem thank you, you are right. I fixed it.
– Robert Dresler
Nov 13 '18 at 17:29
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
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
@DarrenJay yes, you are right! :-)
– Robert Dresler
Nov 13 '18 at 18:59
@DarrenJay yes, you are right! :-)
– Robert Dresler
Nov 13 '18 at 18:59
|
show 1 more comment
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]
}
}
add a comment |
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]
}
}
add a comment |
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]
}
}
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]
}
}
answered Nov 13 '18 at 17:13
impression7vximpression7vx
49211036
49211036
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 13 '18 at 17:30
Viren MalhanViren Malhan
755
755
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286075%2frandom-numbers-in-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Have a look here
– Carpsen90
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