# Random generated puzzel and no number may be the same

Page 1 of 1

## 3 Replies - 342 Views - Last Post: 16 November 2011 - 05:47 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=255888&amp;s=07e1645dc8883b2d7c62eab2dd48a0e5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cornetto456

• D.I.C Regular

Reputation: 21
• Posts: 438
• Joined: 03-January 11

# Random generated puzzel and no number may be the same

Posted 16 November 2011 - 02:16 AM

Hi,

I have a friend that's in university and he asked me I could help him a bit, so I did that.

He has an assigment where you need to make a puzzel and make output like this:
|--------------------------------|
| 6 | 3 | 3 | 9 |
| 0 | 4 | 10 | 5 |
| 8 | 10 | 1 | 11 |
| 5 | 13 | 2 | 0 |
|--------------------------------|
But the problem is that some number's are the same. and that's not allowed. (2 time's 10, 2 time's 3)

And I don't now how i should fix that.

Here i generate the bord:
package puzzel.slidepuzzle;

import java.util.Arrays;
import java.util.Random;
/*
*
* Calculation:
* List all numbers on the board lexically, i.e., left to right and top to bottom.
For each number n in this list, count the number of inversions: the numbers that precede n
in the list and that are larger than n.
If the total number of inversions, added over all numbers in the list, is even then the puzzle
is solvable. If it's odd, the puzzle can't be solved.
*  int i = Arraylist[huide] -1;
*
*  Formule:
*
*
*/
public class ConfigGenerator {
private Random randomizer= new Random();
final int rowWidth  = 4;
final int rowHeight = 4;
private int result[][];

public int[][] getRandomConfig(){

int result[][] = new int[rowWidth][rowHeight];
for (int row = 0; row < result.length; row++) {

for (int col = 0; col < result[row].length; col++) {

result[row][col]  = randomizer.nextInt(15);

}

}

for(int i = 0; i < result.length; i++) {

for(int j = 0; j < result[i].length; j++) {

System.out.print(" |  " + result[i][j] + "\t" + " ");

//System.out.println();

}
System.out.print(" | ");

System.out.println();

}

return result;

}

public boolean isSolvable(int[][] config) {

return false;

}
}

In the board class I call this class and run getRandomConfig()

Thanks!

Is This A Good Question/Topic? 0

## Replies To: Random generated puzzel and no number may be the same

### #2 mostyfriedman

• The Algorithmi

Reputation: 727
• Posts: 4,473
• Joined: 24-October 08

## Re: Random generated puzzel and no number may be the same

Posted 16 November 2011 - 03:12 AM

you can maintain a hashmap or any structure with O(1) access time. Each time you generate a number, check if its in the hashmap, if it is, then generate another one, if its not then set the number to the current cell and save the number in the hashmap.

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Random generated puzzel and no number may be the same

Posted 16 November 2011 - 05:00 AM

It is just generating Lotonumbers from 0 to 15

Fill an ArrayList with number 0 to 15
Randomly remove numbers from that ArrayList

http://www.dreaminco...snippet2992.htm

ArrayList<Integer> al = new ArrayList<Integer>();
for(int i = 0; i < 16; ++i)

Random ran = new Random();
int[][] result = new int[4][4];
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
int index = ran.nextInt(al.size());
result[i][j] = al.remove(index);
}
}

### #4 Cyclopses

Reputation: 5
• Posts: 38
• Joined: 20-January 11

## Re: Random generated puzzel and no number may be the same

Posted 16 November 2011 - 05:47 AM

pbl, on 16 November 2011 - 01:00 PM, said:

It is just generating Lotonumbers from 0 to 15

Fill an ArrayList with number 0 to 15
Randomly remove numbers from that ArrayList

I would advise the first reply, using a hashmap, the above solution isn't exactly dynamic in scale. Although you could make it so, don't copy paste.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }