Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating all discrete permutations

Status
Not open for further replies.

Mantle51

Programmer
Aug 17, 2006
97
US
Hello,

In a spreadsheet I have 4 categories running vertically with 4 items in each -16 cells. (They're excersies that are grouped by type) So what I've attempted to do is create a random selector of exercises; one from each group. It will initiate using the Seconds portion of the Now function -this I did not include. My initial thought was to take 4 factorial x 4 factorial for a total of 576 permutations, but I'm not interested in all possible sequences within a combination, only the unique selection as a whole. For example; Push Up, Pull Up, Dip and Sit Up would be one unique combination and I'm not concerned with order. And so I think factorial includes order.
Anyway, what I came up with was 256 discrete combinations by comparing two groups at a time for 16 possible combinations(disregarding order), then compared those 16 versus second group of 16. Initially I tried to code it as one big ugly set of For Nexts, but realized it was an enormously daunting task.
The following works, but I wonder if anyone has a different take on combination algorithms.

Thanks.......Mickey

Dim PartCmb1(32), PartCmb2(256), Some1(4), Some2(4), Some3(4), Some4(4), Some5(4)
Dim i As Integer, j As Integer, k As Integer, m As Integer


For j = 1 To 4
Some1(j) = Worksheets("Exercise").Range("B" & j).Value
Some2(j) = Worksheets("Exercise").Range("B" & j + 4).Value
Some3(j) = Worksheets("Exercise").Range("B" & j + 8).Value
Some4(j) = Worksheets("Exercise").Range("B" & j + 12).Value

Next

k = 1
For i = 1 To 4
For j = 1 To 4
PartCmb1(k) = Some1(i) & ", " & Some2(j)
k = k + 1
Next j
Next i

For i = 1 To 4
For j = 1 To 4
PartCmb1(k) = Some3(i) & ", " & Some4(j)
k = k + 1
Next j
Next i

''''Combines fist two groups
j = 16
m = 1
For k = 1 To 16
For i = 1 To 16
PartCmb2(m) = PartCmb1(i) & ", " & PartCmb1(j)
m = m + 1
Next i
j = j + 1
Next k

For m = 1 To 256
Cells(m, 6).Value = PartCmb2(m)
Next
 
Here's an alternative. Whether it is better is open to question


For lp = 1 To 4
For lp2 = 1 To 4
For lp3 = 1 To 4
For lp4 = 1 To 4
mycount = mycount + 1
Cells(mycount, 6) = Worksheets("Exercise").Range("B" & lp).Value & Worksheets("Exercise").Range("B" & lp2 + 4).Value & Worksheets("Exercise").Range("B" & lp3 + 8).Value & Worksheets("Exercise").Range("B" & lp4 + 12).Value
Next
Next
Next
Next
 
Thank you StongM,

You know that was my original idea, but then I over-thought the sequence and for some reason feared that nesting all the For-Next would not pick up every combination.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top