How to be sure that random numbers are unique and not duplicated?

后端 未结 5 1580
隐瞒了意图╮
隐瞒了意图╮ 2020-12-17 06:23

I have a simple code which generates random numbers

SecureRandom random = new SecureRandom();
...
public int getRandomNumber(int maxValue) {
    return rando         


        
5条回答
  •  -上瘾入骨i
    2020-12-17 06:53

    For such a small number of possible values, a trivial implementation would be to put your 1000 integers in a list, and have a loop which, at each iteration, generates a random number between 0 and list.size(), pick the number stored at this index, and remove it from the list.

提交回复
热议问题