Home > Software design >  How to reset a Random Number in Kotlin
How to reset a Random Number in Kotlin

Time:03-11

I tried to make Guess the Number game that the number generate using this code:

var randomNumber: Int = Random().nextInt(50)

and not only that, I tried to put the retry button and reset function:

        fun initializeGame(){
        status.text = ""
        tGuess.text = ""
        randomNumber
        btnGuess.isEnabled = true
        guessCount = 0
        tCounter.text = "The Chances Remain: $guessCount / $guessLimit"
    }

The problem is I can't reset the random generator. Sorry for the bad explanation. I'm not very good at English.

CodePudding user response:

You can do randomNumber = Random().nextInt(50) as your randomNumber is var.

Also, not to create Random object every time, you could store it in variable and reuse that instead, static variable preferably.

  • Related