1

If I do this, it sometimes sets the same image as it was in image_view. For example, if R.drawable.cat__5_ is in image_view I want to select from R.drawable.cat__3_,R.drawable.cat__4_,R.drawable.cat__6_,R.drawable.cat__7_,R.drawable.cat__8_. How it can be done?

lateinit var random: Random
random = Random()
        var cats = intArrayOf(
                        R.drawable.cat__3_,
                        R.drawable.cat__4_,
                        R.drawable.cat__5_,
                        R.drawable.cat__6_,
                        R.drawable.cat__7_,
                        R.drawable.cat__8_)

            button.setOnClickListener{

            image_view.setImageResource(cats[random.nextInt(cats.size)])
    }
Pranciskus
  • 487
  • 1
  • 6
  • 17
  • there are multiple ways of doing it. One way would be create a pojo class with isAlreadySet and image keys and update isAlreadySet key to true when image is set. Then for the next time you can filter out based on isAlreadySet and select a random from the filtered list. – Karthik Dec 26 '19 at 11:31
  • The object for random should be created only once rather than creating every time. Thereby I would suggest having the random variable as member and create it lazily. try this private val random by lazy { new Random() } and let me know – Raghul Vaikundam Dec 26 '19 at 14:25
  • @vasik988 does the above suggestion works?. Do you need any pattern to display the image or does it need to display randomly? – Raghul Vaikundam Dec 26 '19 at 18:11
  • Thank you for comments. @VaikundamRaghul I changed my code to be like one I use. I tried to place your code (`private val random by lazy { new Random() }`) at the top level (outside class). But it showed me that "new" is unresolved reference. And I don't really understand how it can solve my issue. – Pranciskus Dec 27 '19 at 15:49
  • Oops, sorry `private val random by lazy { Random() }` is sufficient. `new` not required. Because creating Random everytime with the default seed value will give the same value. For your reference take a look at this link https://stackoverflow.com/a/5533203/9584758. I hope your issue should get resolved. let me know if it is working – Raghul Vaikundam Dec 27 '19 at 16:28
  • @VaikundamRaghul Thanks, but no. Sometimes the same image is repeated anyway. My goal is to pick a random image but don't repeat one previous. – Pranciskus Dec 27 '19 at 17:01
  • @vasik988 Added a solution to your problem and can you try and accept if it is working – Raghul Vaikundam Dec 28 '19 at 06:27

1 Answers1

1
private var previousIndex = 0  // member variable to track the previous index
private val random by lazy { Random() }
private val cats = intArrayOf(
                        R.drawable.cat__3_,
                        R.drawable.cat__4_,
                        R.drawable.cat__5_,
                        R.drawable.cat__6_,
                        R.drawable.cat__7_,
                        R.drawable.cat__8_)

button.setOnClickListener{       
            image_view.setImageResource(cats[getNewRandomIndex()])
    }


private fun getNewRandomIndex(): Int {
    var newIndex = -1
    while(true) {
        newIndex = random.nextInt(cats.size)
        if(previousIndex != newIndex) {
            previousIndex = newIndex
            break
        }
    }
    return newIndex
}

The above function getNewRandomIndex() returns the index which is always different from the recent index. So you will get different image each time

Raghul Vaikundam
  • 588
  • 4
  • 20