montecarlo2012
Well-known Member
- Joined
- Jan 26, 2011
- Messages
- 986
- Office Version
- 2010
- Platform
- Windows
VBA Code:
Sub Monte_carlo_Mr_excel()
On Error GoTo errHandler
Dim rRng As Range, p As Integer, lRow As Long
Dim vElements, vresult As Variant
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
lRow = 0
Set rRng = Range("A1:A" & lastRow)
rRng.Select
p = Range("C19")
If CInt(Range("C19")) = 0 Then
MsgBox "Enter amount of numbers to be in combinations"
Exit Sub
End If
Dim q As Integer
Dim b As Double
b = 1
For q = 0 To p - 1
b = b * (lastRow - q) / (p - q)
Next q
MsgBox "No of Non repeating combinations is -> " & b, vbInformation
vElements = Application.Index(Application.Transpose(rRng), 1, 0)
ReDim vresult(1 To p)
Columns("D").Resize(, p + 15).Clear
Call CombinationsNP(vElements, p, vresult, lRow, 1, 1)
Exit Sub
errHandler:
MsgBox "Error has occured - error no " & Err.Number & " - " & Err.Description
End Sub
Sub CombinationsNP(vElements As Variant, p As Integer, vresult As Variant, lRow As Long, iElement As Integer, iIndex As Integer)
Dim i As Integer, k As Integer
On Error GoTo errHandler
For i = iElement To UBound(vElements)
vresult(iIndex) = vElements(i)
If iIndex = p Then
For k = LBound(vresult) To UBound(vresult)
Next
lRow = lRow + 1
Range("D" & lRow).Resize(, p) = vresult
End If
testRow = testRow + 1
If iIndex <> p Then
Call CombinationsNP(vElements, p, vresult, lRow, i + 1, iIndex + 1)
End If
Next i
Exit Sub
errHandler:
MsgBox "Error has occured - error no " & Err.Number & " - " & Err.Description
End Sub
ZMUSOEN choose from 3 to 6.xlsm | ||||||||
---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | |||
1 | 0 | 0 | 8 | |||||
2 | 8 | 0 | 5 | |||||
3 | 5 | 0 | 2 | |||||
4 | 2 | 0 | 7 | |||||
5 | 7 | 0 | 1 | |||||
6 | 1 | 0 | 3 | |||||
7 | 0 | 4 | ||||||
8 | 0 | 9 | ||||||
9 | 8 | 5 | ||||||
10 | 8 | 2 | ||||||
11 | 8 | 7 | ||||||
12 | 8 | 1 | ||||||
13 | 8 | 3 | ||||||
14 | 8 | 4 | ||||||
15 | 8 | 9 | ||||||
16 | 5 | 2 | ||||||
17 | 5 | 7 | ||||||
18 | 5 | 1 | ||||||
19 | Enter Number in Combination - | 2 | 5 | 3 | ||||
20 | 5 | 4 | ||||||
21 | 5 | 9 | ||||||
22 | 2 | 7 | ||||||
23 | 2 | 1 | ||||||
24 | 2 | 3 | ||||||
25 | 2 | 4 | ||||||
26 | 2 | 9 | ||||||
27 | 7 | 1 | ||||||
28 | 7 | 3 | ||||||
29 | COUNT ELEMENT FREQ. | 7 | 4 | |||||
30 | 1 | 7 | 9 | |||||
31 | 1 | 1 | 3 | |||||
32 | 1 | 1 | 4 | |||||
33 | 1 | 1 | 9 | |||||
34 | 1 | 3 | 4 | |||||
35 | 1 | 3 | 9 | |||||
36 | 1 | 4 | 9 | |||||
37 | 1 | |||||||
38 | 1 | |||||||
39 | 1 | |||||||
40 | 1 | |||||||
41 | 1 | |||||||
42 | 1 | |||||||
43 | 1 | |||||||
44 | ||||||||
45 | ||||||||
Combination Generation |
Cell Formulas | ||
---|---|---|
Range | Formula | |
B30 | B30 | =COMBIN(COUNT($A$2:$A$16),D1) |
B31:B43 | B31 | =COMBIN(COUNT($A$2:$A$16),C31) |
Named Ranges | ||
---|---|---|
Name | Refers To | Cells |
'Combination Generation'!_FilterDatabase | ='Combination Generation'!$A$2:$A$461 | B30:B43 |
from here I would like to have control on
how many time each element must be in the comb.
and how namy comb. I want.
like this:
ZMUSOEN choose from 3 to 6.xlsm | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | 0 | element freq. | 6 | 0 | 8 | ||
2 | 8 | 5 | 0 | 5 | |||
3 | 5 | 4 | 0 | 2 | |||
4 | 2 | 4 | 0 | 7 | |||
5 | 7 | 3 | 0 | 1 | |||
6 | 1 | 3 | 0 | 9 | |||
7 | 9 | 0 | 4 | ||||
8 | 4 | 8 | 5 | ||||
9 | 8 | 2 | |||||
10 | 8 | 7 | |||||
11 | 8 | 1 | |||||
12 | 8 | 9 | |||||
13 | 8 | 4 | |||||
14 | 5 | 2 | |||||
15 | 5 | 7 | |||||
16 | 5 | 1 | |||||
17 | 5 | 9 | |||||
18 | 5 | 4 | |||||
19 | enter number of digist | 2 | 2 | 7 | |||
20 | enter number of comb. | 13 | 2 | 1 | |||
21 | 2 | 9 | |||||
22 | 2 | 4 | |||||
23 | 7 | 1 | |||||
24 | 7 | 9 | |||||
25 | 7 | 4 | |||||
26 | 1 | 9 | |||||
27 | 1 | 4 | |||||
28 | 9 | 4 | |||||
29 | COUNT ELEMENT FREQ. | ||||||
30 | 1 | 0 | |||||
31 | #NUM! | 8 | |||||
32 | 21 | 5 | |||||
33 | 21 | 2 | |||||
34 | 1 | 7 | |||||
35 | 7 | 1 | |||||
36 | 1 | ||||||
37 | 1 | ||||||
38 | 1 | ||||||
39 | 1 | ||||||
40 | 1 | ||||||
41 | 1 | ||||||
42 | 1 | ||||||
43 | 1 | ||||||
44 | |||||||
45 | |||||||
Combination Generation |
Cell Formulas | ||
---|---|---|
Range | Formula | |
B30:B43 | B30 | =COMBIN(COUNT($A$2:$A$16),C30) |
Named Ranges | ||
---|---|---|
Name | Refers To | Cells |
'Combination Generation'!_FilterDatabase | ='Combination Generation'!$A$2:$A$461 | B30:B43 |
in front of 0 on C is 6 meaning I would like 0 to show up 6 times etc.
Thank you for your time,