I'm trying to get this part of the app to work were the user clicks a button and a label prints a randomly generated number between 1-12. I've been able to successfully do that, but I also want it to not repeat any random numbers that have already been printed.
What I've tried doing is putting any printed number into an array, and then checking the array each time it generates a new number.
I've gotten it to work in the Playground, but cannot get it working with a real project.
Here is the code for my project.
var usedNumbers = [Int]()
var randomConv = 0
func randomize() {
lblRandom.text = "\(arc4random_uniform(12) + 1)"
randomConv = Int(lblRandom.text!)!
}
@IBAction func btnRandomPressed(sender: AnyObject) {
randomize()
if usedNumbers.contains(randomConv) {
randomize()
} else {
usedNumbers.append(randomConv)
}
if usedNumbers.count == 12 {
btnRandom.hidden = true
}
}
And here is the code from my playground.
var lblRandom = "\(arc4random_uniform(12) + 1)"
var randomConv = 0
var usedNumbers = [Int]()
func randomize() {
lblRandom = "\(arc4random_uniform(12) + 1)"
randomConv = Int(lblRandom)!
}
repeat {
randomize()
if usedNumbers.contains(randomConv) {
randomize()
} else {
usedNumbers.append(randomConv)
print(lblRandom)
}
} while usedNumbers.count < 12
I´m not quite sure why your code is not working, but try this code instead this works.
var usedNumbers = [Int]()
var randomConv = 0
@IBAction func btnRandomPressed(sender: AnyObject) {
randomConv = Int(arc4random_uniform(12) + 1)
if usedNumbers.contains(randomConv) {
// If you find a duplicate you fire the event again and a new number will be randomized
print("Exists \(randomConv)")
btn_Pressed.sendActionsForControlEvents(.TouchUpInside)
} else {
print(randomConv)
lblTest.text = String(randomConv)
usedNumbers.append(randomConv)
}
}
Update
When the usedNumbers.contains(randomConv)
condition is true
, you can use this row to fire the button event again: btn_Pressed.sendActionsForControlEvents(.TouchUpInside)
- btn_Pressed is your buttons outlet from your Storyboard.
I have updated the code block so that you can see a fully working example.
Update 2, An alternative solution
func randomize(){
repeat {
if (usedNumbers.count == 12){
return
}
randomConv = Int(arc4random_uniform(12) + 1)
} while usedNumbers.contains(randomConv)
usedNumbers.append(randomConv)
lblTest.text = String(randomConv)
}
@IBAction func btnRandomPressed(sender: AnyObject) {
randomize()
}