How to have unique random number?

后端 未结 7 813
情深已故
情深已故 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:16

    Generate a list of numbers containing every number you will use. (Which is fine, given that we are talking about a small range, where "N" is "somewhere less than a thousand")

    When you choose a number, select a random index between 0 and sizeof(list), that number becomes the index of that list.

    Delete that list index and return the number.

    (It is an exercise to the reader to determine what kind of "list" is appropriate here.)

提交回复
热议问题