Having a problem

hi there,
I was building an app in which if u will tap the button then it will show random numbers between 1 to 100 and print “App is working”.
when I am tapping the button once the number is randomised and the print statement came to the console but when I am tapping the button again, the print statement is coming but the number is not.
so can u please help me with this problem.

I have attached a screenshot having the code I wrote till now

Hi @Siddharth1

Place your var randomNumber = Int.random(in: 1...100) inside the @IBAction like this:

@IBAction func ButtonToGetRandomNumbers(_ sender: Any) {
    var randomNumbers = Int.random(in: 1...100)
    randomNumberLabel.text = String("\(randomNumbers)"
    print("app is working")
}

so that each time you tap the button it generates a different number.

Each time you press the button it will only generate 1 randomNumber so given that it is a single number you should name your variable randomNumber rather than randomNumbers. It also should be declared as a let rather than a var since you are not changing it anywhere else in your code.

Hope that helps.

1 Like