You don't want random values for your sliding puzzle.

You want randomized POSITIONS for your sliding puzzle game. (and I have a bit of bad news for you about that, later on).

So to answer your question:

Code:

1) set up your 2D array with 1 to 15:
for(r=0;r<4;r++) {
for(c=0;c<4;c++) { //r=row, c=column
puz[r][c]=r*4+c+1;
}
}

Now generate some random INDEX between and swap the values around - like you might shuffle cards, but use something like:

Use srand() once, up here first, then

Code:

for(r=0;r<4;r++) {
for(c=0;c<4;c++) {
//generate a random index between 0 and 14, putting it
//r1 and c1 variables. Now swap puz[r1][c1] with puz[r][c].
//using a temp value, of course.
}
}

There are more randomized ways to do this, but this is quite serviceable.

It's easy, and you'll have no repeating numbers in the puzzle.

The way this sliding puzzle works, it has "hands" property. To be solvable, your original layout of numbers, must be (call it right handed, I'm not sure what the experts call it now, right or left handedness).

So half the layouts you might give a puzzle, will not be solvable, no matter what you do. Half will be solvable.

Google "15 sliding puzzle", and you can read up on how to ensure your original puzzle has the right "hand" layout.