How to generate a pair of random number from 1 to 8 that can fill into a 2D array in a 4x4 square?
For example:
2 3 5 6
1 7 8 3
5 4 1 6
7 2 4 8
How to generate a pair of random number from 1 to 8 that can fill into a 2D array in a 4x4 square?
For example:2 3 5 6
1 7 8 3
5 4 1 6
7 2 4 8
The best way is to use the Random.randomInt(...) method:
Random r = new Random();
int[][] a = new int[4][4];
for(int i=0; i<a.length; i++){
for(int j=0; j<a[0].length; j++){
a[i][j] = r.randomInt(7)+1;
}
}
:)
Andrej
The best way is to use the Random.randomInt(...) method:
Random r = new Random(); int[][] a = new int[4][4]; for(int i=0; i<a.length; i++){ for(int j=0; j<a[0].length; j++){ a[i][j] = r.randomInt(7)+1; } } :) Andrej
But how I gona to make sure every number from 1 to 8 are come out in a pairs?
But how I gona to make sure every number from 1 to 8 are come out in a pairs?
I misunderstood your quastion....
explain waht do you mean pair?
some thing like:
Array[Pair(random-number1, random-number2), Pair(random-number1, random-number2), ...] ?
But how I gona to make sure every number from 1 to 8 are come out in a pairs?
You would just use the method randomInt twice to get a pair. If you mean that you wouldn't want a duplicate value in your pair, then you would keep calling it until you didn't have two of the same values in your pair.
I misunderstood your quastion....
explain waht do you mean pair?
some thing like:
Array[Pair(random-number1, random-number2), Pair(random-number1, random-number2), ...] ?
Something like a matching game like that...
Thanks you all...
I solved it already...
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.