random numbers not repeated java [duplicated]

0

Hello, how would you assure that this code did not generate two identical random numbers? is an array of 6 rows and 10 columns to which I assign random numbers from 1 to 1000

int[][] num = new int[6][10];
for(int f=0;f<=5;f++){
        for(int c=0;c<=9;c++){
            num[f][c]=(int) (Math.random()*1000+1);
        }
    }
    
asked by tdpt 10.12.2018 в 08:57
source

1 answer

0

You can create a number register array and check if the random released is in the registry, if it repeats the operation but you register in your matrix and continue:

ArrayList<int> registry = new ArrayList<int>();
int random;
int[][] num = new int[6][10];

for(int f=0;f<=5;f++){
    for(int c=0;c<=9;c++){
        do{
            random = (int) (Math.random()*1000+1);
        }while(!IntStream.of(registry).anyMatch(x -> x == random));
        registry.add(random);
        num[f][c]= random;
    }
}
    
answered by 10.12.2018 / 11:43
source