So I'm looking for a way to assign a set of numbers in a random way in a 2d array.

The array is {1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10}

How would I assign these values randomly in a 5 row and 4 column array?

Or is there a way to generate a random number for each location in the 2D array, but if I do this then there has to be the same random number in another location as well. So there has to be pairs of numbers generated.

## All 4 Replies

See the Random class for a way to generate random numbers.

a way to generate a random number for each location in the 2D array

What are you asking here? Are you trying to randomly access an element in the array,
or are you trying to generate a random number to put into the array?

If you are trying to randomly access the contents of a 2-dim array, think of the array as one dimensional using the equation: row nbr * nbr columns per row + column number
If you have problems seeing how this works, take a piece of paper and draw a grid of rows and columns and write in each square its x,y location and a sequential number from 0 to the number of boxes across the columns and down the rows. Something like the following for 2 rows and 3 cols:
0 1 2
3 4 5

The array is {1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10}

How would I assign these values randomly in a 5 row and 4 column array?

If the values are already defined in an array you could use the Random() to generate numbers from 0 to 19 and then assign the value from the current index of the array (depending on the random number generated) to a 2d array
In this method you need to check if the random number is already used so as not to assign a duplicate value

or

You could make random numbers from one to ten then check if the number hasn't been used more than twice then assign them in the 2d array... which is a lot easier

So I'm looking for a way to assign a set of numbers in a random way in a 2d array.

The array is {1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10}

How would I assign these values randomly in a 5 row and 4 column array?

Or is there a way to generate a random number for each location in the 2D array, but if I do this then there has to be the same random number in another location as well. So there has to be pairs of numbers generated.