Sign in to follow this  

filling double dimensional arrays

This topic is 4339 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

can anyone tell me the logic ..how to fill double dimensional array with random numbers so that it may not contain duplicates in any row or column like 123 231 312

Share this post


Link to post
Share on other sites
Quote:
Original post by TEUTON
can anyone tell me the logic ..how to fill double dimensional array with random numbers so that it may not contain duplicates in any row or column like
123
231
312
That example isn't really random; it's just a series of cyclic permutations. Perhaps you could provide more info, such as whether the array will always be square, whether the range of the entries is restricted to the dimension (as in your example), whether they should always be integers (I assume so), and whether or not they need to be truly random, or can be a pattern like in the above case.

Share this post


Link to post
Share on other sites
1.Yes arrays will always be sqaure
2.Range of the entries is restricted to the dimension(1 TO N), N being the dimension
3.Yes they should always be integers.

Share this post


Link to post
Share on other sites
It depends upon exactly what you are doing. With your example you could simply generate it as you did then randomly swap entire rows and columns. Whether that is a valid solution depends upon whether you can say in advance what values will be present and all you need is for their precise location to be randomly selected.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Simple. If you never use the same number twice at all, there will be no duplication in the columns nor in the rows.
I think he said the range for the 'random' numbers needed to be restricted to the dimension of the array (may have misunderstood though).

[Edit: Oops, didn't even think about this being homework...]

Share this post


Link to post
Share on other sites
I'm smelling homework here. :p

Well, you could do something like this (it's like solving (or actually, creating) sudoku):

For each of your cells, create a list, named Possible. Also, create two global lists, History and Tried (they start out empty). Fill all the Possible lists with all your numbers (1-N). Then, randomize a number and a cell. If the number/cell pair can be found in the Tried list, or the number is not in the selected cell's possible list, rerandomize. Clear the possible list for that cell and add the number(and the cell where it was stored) to the History list. Loop through the row and column you added the number to, and remove the number from their Possible list. Repeat this process, adding random numbers to random cells. You might get a successful solution on the first run, but probably not. This is why you keep track of all additions in the History list. When you get in a dead-end, remove the last item from the History list and add it to the Tried list. This is like "going back" from a nonpossible solution. Finally you should have a grid that is fully filled.

* edited..

Share this post


Link to post
Share on other sites
Oh and as a side note, the algorithm described here is a "trial-and-error" one and doesn't perform very fast.. if you google around, there are more sophisticated algorithms (and better described than what I did) available for creating sudokus.

Share this post


Link to post
Share on other sites

This topic is 4339 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this