0

I'm trying to fill an array with random numbers. There must be unique. I've tried this:

int random(int[] arr) {
    int number = 0;
    for (int i = 0; i < arr.length; i++) {
        Random rn = new Random();
        number = rn.nextInt(100);
        for (int j = i; j>0; j--) {
            if(arr[j]==zahl){
                random(arr);
            }
        }
    }
    return number;
}

I would be thankfull if you could help with my problem.

gjelbrim
  • 3
  • 1
  • 1
    Fill the array with the values you want, then shuffle as described in the linked post. – Jim Garrison Jan 10 '18 at 01:27
  • To ensure distinct values, fill a `Set` which automatically eliminates duplicates. Then feed that `Set` to constructor of a `List`. – Basil Bourque Jan 10 '18 at 01:35
  • @JimGarrison - _"...with the values you want,"_ OP doesn't know the values, they are _random_. It it not a random _order_ that's wanted, as `shuffle` would do, it is random _contents_ that OP is after. Basil's comment to use a `Set` would do well for that. – Stephen P Jan 10 '18 at 01:47
  • This question is already been answered here: https://stackoverflow.com/questions/8115722/generating-unique-random-numbers-in-java – Kushal Shinde Jan 10 '18 at 03:47

0 Answers0