# Generating random pairs of numbers in a 2d array (1-8)

Page 1 of 1

## 4 Replies - 534 Views - Last Post: 26 May 2012 - 10:23 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=280642&amp;s=aa75beafcebcfc0d65c68945e310df65&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 amy_rules

Reputation: 0
• Posts: 3
• Joined: 25-May 12

# Generating random pairs of numbers in a 2d array (1-8)

Posted 25 May 2012 - 06:26 AM

Hi guys. I'm trying to create a so called matching pairs game for my assignment. And so far I've managed to to create a 4X4 array and filled it with pairs of 1-8. What I need help with is how do it randomize the positioning of the pairs in the array.

Any help would be greatful.

```import java.util.Scanner;

public class Memory
{

int[][] initialValue = {{1, 2, 3, 4}, {5, 6 ,7 , 8},{1, 2, 3, 4}, {5, 6 ,7 , 8}};

boolean[][] displayed = new boolean[4][4];
Scanner scan;

Memory()
{
boolean gameIsOver = false;
scan = new Scanner(System.in);
while(!gameIsOver)
{
System.out.println("X represents the row which is downwards. Y represents the columns which is across. Example the" +
" coordinates for the top left is X = 0, Y = 0");
System.out.println();

displayCard();

System.out.print("Enter coordinates X to show: ");
int x = scan.nextInt();

System.out.print("Enter coordinates Y to show: ");
int y = scan.nextInt();
displayed[x][y] = true;
System.out.println();

}

}

void displayCard()
{
Random randomCards = new Random();
System.out.println("The Cards:");

for(int j = 0; j < 4; j++)
{
for(int i = 0; i < 4; i++)
{
if(displayed[i][j])

System.out.print(" " + initialValue[i][j] + " ");
else
System.out.print(" * ");
}
System.out.println();
}
}

public static void main(String[]args)
{
new Memory();
}

}

```

Is This A Good Question/Topic? 0

## Replies To: Generating random pairs of numbers in a 2d array (1-8)

### #2 amy_rules

Reputation: 0
• Posts: 3
• Joined: 25-May 12

## Re: Generating random pairs of numbers in a 2d array (1-8)

Posted 25 May 2012 - 06:43 AM

The output would be something like:

* * * *
* * * *
* * * *
* * * *

### #3 amy_rules

Reputation: 0
• Posts: 3
• Joined: 25-May 12

## Re: Generating random pairs of numbers in a 2d array (1-8)

Posted 25 May 2012 - 06:49 AM

The user would then be asked to enter the coordinate for "X" which is the row and "Y" which is the columns.

Once the user has entered the coordinates, the "cards" would be so called face up. What i'm trying to figure out is how do I randomize the positioning of the number pairs. So far the output once the "cards" have been revealed it would be like this:

1 5 1 5
2 6 2 6
3 7 3 7
4 8 4 8

### #4 g00se

• D.I.C Lover

Reputation: 2148
• Posts: 8,925
• Joined: 20-September 08

## Re: Generating random pairs of numbers in a 2d array (1-8)

Posted 25 May 2012 - 08:16 AM

http://technojeeves....e-array-in-java

You can also use List<Integer> instead and call Collections.shuffle

### #5 gattigen

Reputation: 3
• Posts: 4
• Joined: 29-March 12

## Re: Generating random pairs of numbers in a 2d array (1-8)

Posted 26 May 2012 - 10:23 AM

Dont you need to import the random class (import java.util.Random)