Jump to content
  • Advertisement
Sign in to follow this  
TEUTON

filling double dimensional arrays

This topic is 4555 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
Advertisement
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
Simple. If you never use the same number twice at all, there will be no duplication in the columns nor in the rows.

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
Yes you can call it an assignment or homework. But I never asked for a code from you guys.. Just asking for logic. Thanks everyone especially clb

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!