random number generator from a range for continuos analysis

前端 未结 2 864
不思量自难忘°
不思量自难忘° 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<Integer> 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.

    0 讨论(0)
  • 2021-01-23 12:58

    I do not think this is possible. Try to remember the numbers you already generatedin a list and use it to generate more random numbersuntil you find one that is not in your list.

    0 讨论(0)
提交回复
热议问题