How to have unique random number?

后端 未结 7 831
情深已故
情深已故 2020-12-16 08:38

This is how i am generating a unique no in between 1 to 6 and getting appropriate images from the drawable folder.

Random rand = new Random();
// n = the n         


        
7条回答
  •  渐次进展
    2020-12-16 09:08

    The usual approach to this kind of problem is to create a list containing each of the possible values and shuffle it (use Collections.shuffle). You then consume one item from the list each time you need a value. This will ensure that you don't use the same value more than once but still allows for a random order.

提交回复
热议问题