Generating Unique Random Numbers in Java

后端 未结 21 2564
不思量自难忘°
不思量自难忘° 2020-11-21 07:45

I\'m trying to get random numbers between 0 and 100. But I want them to be unique, not repeated in a sequence. For example if I got 5 numbers, they should be 82,12,53,64,32

21条回答
  •  悲哀的现实
    2020-11-21 08:06

    1. Create an array of 100 numbers, then randomize their order.
    2. Devise a pseudo-random number generator that has a range of 100.
    3. Create a boolean array of 100 elements, then set an element true when you pick that number. When you pick the next number check against the array and try again if the array element is set. (You can make an easy-to-clear boolean array with an array of long where you shift and mask to access individual bits.)

提交回复
热议问题