Home > Blockchain >  Kotlin - Random gives same results every time I relaunch app
Kotlin - Random gives same results every time I relaunch app

Time:11-21

I have a simple app that should generate a random string out of an Array. Now what happens is that when I start the app, every time, I het the same results. It happens on a simulator and real device.

Now I found different things on the internet. Some say its a bug, others say its default. Other say you should set a random seed, but how should I do that?

How can I fix this to make sure I get a different string every time.

This is my code:

class SmarttechtransKaartActivity : AppCompatActivity() {
 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_smarttechtrans_kaart)


val Carts = arrayOf(""  
                "1. goobzrort",

            "2. yweagS",

            "3. ikhrescelte toau",

            "4. erecksliteh daeltoue",

            "5. jzdrfnlieed vrutioeg",

            "6. itdnrre-3p",

            "7. rp-i4rtedn",

            "8. edron",

            "9. uenkifser",

            "10. ligeovdl srehelikct cihps",

            "11. smigakuetnt eiitelltnegni",

            "12. dlnwoemin",

            "13. bnaardknsepiige",

            "14. dR-coQe",

            "15. aadt",

            "16. nlicohkabc",

            "17. enokeciosninem",

            "18. iuhitlm nio eraibjtt",

            "19. tnaueoozn",

            "20. mstartawch",

            "21. bRlrV-i",

            "22. tirtenne",

            "23. ecaram",

            "24. ldelpma",

            "25. rbotorstufieogz",

            "26. miasceuothta sreagrimaa",

            "27. emslmi tatmtrohsae",

            "28. tpasdoc",

            "29. rerirfya",

            "30. eradoazld drealpo",

            "31. creietttiilek",

            "32. GSP",

            "33. ntoiibc",

            "34. rnttInee of Tighsn",

            "35. egevrselnearvv",

            "36. citsrhbieeom tnenoatlgcegosro",

            "37. ezdmurua reeieng",

            "38. tiajtreibbo",

            "39. ayesnietetvimags",

            "40. udtsomeinre",

            "41. koeoebrntuk",

            "42. geatmednu aeytlri",

            "43. thootelbu",

            "44. gnfaree",

            "45. cvreleati nudbwola",

            "46. semlmi olksatek",

            "47. nosrse",

            "48. terabee sesonr lpi",

            "49. oormlhag",

            "50. ibg adat",
            )

button.setOnClickListener() {
val random = Carts.random()
tvvoorkant.setText(random)
}

}
}

UPDATE! FIX:

 button.setOnClickListener {

//OLD: Gets same result on relaunch app...
val random = Carts.random()

textview.text = randomValue

//NEW: Gets different result on relaunch app...
val randomString = Random(System.currentTimeMillis())
val randomValue = Carts[randomString.nextInt(Carts.size)]

textview.text = randomValue

CodePudding user response:

One important thing you need to know about random values in general is that they are not random, they are pseudo-random

A computer can not generate random values. What it does is generate a huge sequence of numbers that seems to be random but eventually will repeat. This is where the seed takes relevance

You will usually see people creating Random instances and using the values it gives as if they were random, but since the seed it's using is always the same, the sequence of pseudo-random values will always be the same

For "truly" random values, you need to provide a different seed each time. You can safely use System.currentTimeMillis() for that

**Note that I don't have an IDE right now so the syntax might not be exact. You can rely on auto-complete features for the proper syntax

UPDATE

OK, I checked the docs. Take a look to the Random docs for creating a new Random instance with a custom seed, the random extension function for using it in your arrays and getTimeMillis for getting a unique seed for your Random instances

  • Related