Generate set of unique random numbers in Java

前端 未结 4 984
萌比男神i
萌比男神i 2020-12-03 19:19

I want to create 10 random numbers in the range 0-500. But the problem is that I want those numbers to be unique. For 2 random numbers i could create something as the follow

4条回答
  •  不思量自难忘°
    2020-12-03 19:59

    Make a LinkedList of the numbers from 1-500 and shuffle one out of them each time you use a number using The Fisher-Yates shuffle.

    This will give you guaranteed sane (constant time) performance for each number pulled.

提交回复
热议问题