I would like to generate a 3*6 digits, that can form an array of 3rows*6cols. but I don't want a duplicated to be found in any column. They can be anywhere, in the same row, but in each column, there shouldn't be any duplicate of the same number. Is there an algorithm for generating that ?
Asked
Active
Viewed 202 times
0
-
Consider using a Set - "*A collection that contains no duplicate elements*". – Maroun Jul 24 '13 at 07:39
-
Yes, there are. But SO is not about asking for algorithms - it is for asking questions about specific programming problems. – mthmulders Jul 24 '13 at 07:40
-
mthmulders, algorithms are very much needed for solving specific programming problems. – Emil Vikström Jul 24 '13 at 07:40
-
algorithm for creating random number and checking whether it does not duplicate any of existing variable? Have your tried something by yourself? – Alex Stybaev Jul 24 '13 at 07:41
-
What is the difference between generating three columns and generating one column? If you don't care about duplicates in rows, you're basically asking how to generate a list of numbers without duplicates. That question has been asked *very often* on SO before. – Vincent van der Weele Jul 24 '13 at 07:42
-
@Heuster good point, but the rule that they can be duplicates in rows, but columns no. – andre_lamothe Jul 24 '13 at 07:47
1 Answers
1
Fill the matrix column-wise and use a Set to keep track of which numbers you have already used in the current column. You can use a do-while statement for each cell, in pseudo-code:
for each column {
used = new set
for each cell in column {
do {
num = generate random number
} while (num already in used)
add num to used
add num to the current cell
}
}

Emil Vikström
- 90,431
- 16
- 141
- 175