Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to reset a Random Number in Kotlin

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading