mardi 5 avril 2016

How to generate random numbers using a seed, and populating a 2D array with them

I need help getting random numbers between one and ten using a seed, and then putting them into a 2D array. The numbers must go out two decimal places. Here is an example of what the output must look like:

Please enter the grid size: 3
Please enter the random number seed: 4

7.31 9.19 9.19
6.80 0.78 0.25
6.99 8.05 1.51

Column min values:
6.80 0.78 0.25

Row min values:
7.31
0.25
1.51

Diagonal min value:  0.78

Here is the code I have so far. I know it isn't much.

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.print("Please enter the grid size: ");
    int size = input.nextInt();

    System.out.print("Please enter the random number seed: ");


    double[][] array = new double[size][size];
    System.out.println();
    getArray(array);
    printArray(array);   
}

public static void getArray(double[][] a){
    Scanner input = new Scanner(System.in);
    int seed = input.nextInt();
    Random random = new Random(seed);
    for(int i=0; i<a.length; i++){
        for(int j=0; j<a[i].length; j++){
            a[i][j] = random.nextDouble() * 10;
        }
    }
}

    public static void printArray(double[][] a){
        for(int i=0; i<a.length; i++){
        for(int j=0; j<a[i].length; j++){
            System.out.print(a[i][j] + " ");
        }
        System.out.println();
    }
}
}

This is my output:

Please enter the grid size: 3
Please enter the random number seed: 
1

7.308781907032909 4.100808114922017 2.077148413097171 
3.3271705595951118 9.677559094241207 0.061171822657613006 
9.637047970232077 9.398653887819098 9.471949176631938 

Thank you so much for the help. If you could give me a few pointers on the finding the min values, that would help so much, too.




Aucun commentaire:

Enregistrer un commentaire