mardi 7 mai 2019

Random distinct numbers in nested loop

I am have enrolled in java course and completed nested loops but I was trying to play around with it and wanted to make columns of distinct random numbers but it prints 1 number to all columns. How can I make it so it prints random number to every slot (numbers can be repeated but not copied like its now)

    Random rng = new Random();
    int chance = rng.nextInt((100)+1);
    for (int row = 0; row < 5; row ++) {
        System.out.print("row " + row + ": ");
        for (int col = 0; col < 10; col++) {
            System.out.print(chance + " ");
        }
        System.out.println();
    }

This is the result:

row 0: 13 13 13 13 13 13 13 13 13 13

row 1: 13 13 13 13 13 13 13 13 13 13

row 2: 13 13 13 13 13 13 13 13 13 13

row 3: 13 13 13 13 13 13 13 13 13 13

row 4: 13 13 13 13 13 13 13 13 13 13

but what I need is every number to be randomly generated and not just roll 1 number and put it everywhere.




Aucun commentaire:

Enregistrer un commentaire