0

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

3
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by hket89
0

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
0

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?

0

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), ...] ?

0

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.

0

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...

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.