random number generator from a range for continuos analysis

前端 未结 2 867
不思量自难忘°
不思量自难忘° 2021-01-23 12:19

I can create random numbers from a range using

Random rand = new Random();
int num = rand.nextInt(10);
System.out.println(\"Generated Random Number between 0 to          


        
2条回答
  •  攒了一身酷
    2021-01-23 12:53

    The easiest way would probably be to:

    • put the numbers (0 to 10) in a list
    • call Collections.shuffle() on that list
    • loop over the list

    Something like:

    List numbers = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 10);
    Collections.shuffle(numbers);
    System.out.println(numbers);
    

    You can even provide a random generator for the shuffling operation if the default doesn't suit you.

提交回复
热议问题