ios - How to make sure the generated random number is not equal to the next generated number? -


i generating random number (only 0-2) in swift each time button pressed. don't want generate same number twice in row. did it's not working.

var currentnumber:int = 5 var randomnumber:int = 0  {     randomnumber = int(arc4random_uniform(uint32(3))) } while randomnumber == currentnumber  currentnumber = randomnumber 

you need make randomnumber property of viewcontroller remembered between button presses. each time, there 2 valid random numbers, generate number in range 0 1 , change 2 if matches previous number:

class viewcontroller: uiviewcontroller {     // give randomnumber initial value     var randomnumber = int(arc4random_uniform(uint32(3)))      @ibaction func buttonpressed(button: uibutton) {         let currentnumber = randomnumber         randomnumber = int(arc4random_uniform(uint32(2)))         if currentnumber == randomnumber {             randomnumber = 2         }     } } 

this has advantage of needing 1 random number each button press.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -