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.

Please and thank you.

Recommended Answers

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.

Please and thank you.

well, if you want to randomly assign them, why not just add your existing array to a collection and use the shuffle() method to move the values around and from there and just assign them to the 2d array as if you shuffle them randomly the original array will never be the same once shuffled.

Check this for more clarification:http://www.vogella.de/articles/JavaAlgorithmsShuffle/article.html

I believe what you want to do is iterate through the 2D array with a pair of nested for loops. You could either generate the number as you traverse the array.

I'm not sure what your array description (the {1,1,2,2,3,3,...}) means though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.