vba code generate array.

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
984
Office Version
  1. 2010
Platform
  1. Windows
Generate a VBA code that generate 20 rows of length 5 from a set of 12 numbers, that meet the specific frequency of each number, no permutations.

DISPLAY AT F2:J21

frequency for
1​
equal
8​
frequency for
2​
equal
7​
frequency for
3​
equal
9​
frequency for
4​
equal
10​
frequency for
5​
equal
11​
frequency for
6​
equal
10​
frequency for
7​
equal
11​
frequency for
8​
equal
7​
frequency for
9​
equal
6​
frequency for
10​
equal
5​
frequency for
11​
equal
7​
frequency for
12​
equal
9​
VBA Code:
Sub GenerateCombinations()
    Dim numbers(1 To 12) As Integer
    Dim frequencies(1 To 12) As Integer
    Dim combinations(1 To 20, 1 To 5) As Integer
    Dim i As Integer, j As Integer, k As Integer, n As Integer, count As Integer
    Dim temp As Integer
    
    ' Set the numbers and their frequencies
    numbers(1) = 1
    numbers(2) = 2
    numbers(3) = 3
    numbers(4) = 4
    numbers(5) = 5
    numbers(6) = 6
    numbers(7) = 7
    numbers(8) = 8
    numbers(9) = 9
    numbers(10) = 10
    numbers(11) = 11
    numbers(12) = 12
    
    frequencies(1) = 8
    frequencies(2) = 7
    frequencies(3) = 9
    frequencies(4) = 10
    frequencies(5) = 11
    frequencies(6) = 10
    frequencies(7) = 11
    frequencies(8) = 7
    frequencies(9) = 6
    frequencies(10) = 5
    frequencies(11) = 7
    frequencies(12) = 9
    
    ' Generate combinations
    For i = 1 To 20
        ' Reset count for each combination
        count = 0
        
        ' Generate a combination of 5 unique numbers
        Do While count < 5
            n = Int((12 * Rnd) + 1) ' Generate a random number between 1 and 12
            If frequencies(n) > 0 Then ' Check if frequency of the number is not exceeded
                combinations(i, count + 1) = numbers(n) ' Add the number to the combination
                frequencies(n) = frequencies(n) - 1 ' Decrease the frequency of the number
                count = count + 1 ' Increase the count of numbers in the combination
            End If
        Loop
        
        ' Reset frequencies for the next combination
        For j = 1 To 12
            frequencies(j) = frequencies(j) + 1
        Next j
    Next i
    
    ' Sort the combinations
    For i = 1 To 19
        For j = i + 1 To 20
            For k = 1 To 4
                If combinations(i, k) > combinations(j, k) Then
                    For n = 1 To 5
                        temp = combinations(i, n)
                        combinations(i, n) = combinations(j, n)
                        combinations(j, n) = temp
                    Next n
                ElseIf combinations(i, k) = combinations(j, k) And combinations(i, k + 1) > combinations(j, k + 1) Then
                    For n = 1 To 5
                        temp = combinations(i, n)
                        combinations(i, n) = combinations(j, n)
                        combinations(j, n) = temp
                    Next n
                End If
            Next k
        Next j
    Next i
    
    ' Display the combinations
    Range("F2:J21") = combinations
    
End Sub
this code generate repetition in the rows or permutations, and the frequency is wrong also,
please somebody can check where is the problem.
thank you for reading this
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Does your code produce the right result, if you add the following line at the top of your code:
VBA Code:
Option Base 1

So ist will look like this:
VBA Code:
Option Explicit
Option Base 1

Sub GenerateCombinations()

'... YOUR CODE

End Sub
 
Upvote 0
Hello, PeteWright.
1678772267315.png

the arrow show you why is wrong, the same number twice in the same row, has to be five different numbers or combination, if you check line 8 is even worst, so no it is not working properly.
 
Upvote 0
I will show my effort
VBA Code:
Sub GenerateCombinations()
    Dim numbers(1 To 10) As Integer
    Dim freq(1 To 10) As Integer
    Dim comb(1 To 7, 1 To 5) As Integer
    Dim i, j, k, l, m As Integer
    Dim count As Integer
    
    'initialize the numbers and frequency arrays
    numbers = Array(4, 29, 25, 21, 33, 1, 16, 31, 35, 36)
    freq = Array(4, 4, 4, 4, 3, 3, 4, 3, 3, 3)
    
    'generate the combinations
    For i = 1 To 10
        For j = i + 1 To 10
            For k = j + 1 To 10
                For l = k + 1 To 10
                    For m = l + 1 To 10
                        If freq(i) > 0 And freq(j) > 0 And freq(k) > 0 And freq(l) > 0 And freq(m) > 0 Then
                            comb(count + 1, 1) = numbers(i)
                            comb(count + 1, 2) = numbers(j)
                            comb(count + 1, 3) = numbers(k)
                            comb(count + 1, 4) = numbers(l)
                            comb(count + 1, 5) = numbers(m)
                            freq(i) = freq(i) - 1
                            freq(j) = freq(j) - 1
                            freq(k) = freq(k) - 1
                            freq(l) = freq(l) - 1
                            freq(m) = freq(m) - 1
                            count = count + 1
                            If count = 7 Then Exit Sub
                        End If
                    Next m
                Next l
            Next k
        Next j
    Next i
    
    'output the combinations
    Range("F2:J8").Value = comb
    
End Sub
wrong also
VBA Code:
Sub Gs()
    Dim numbers As Variant
    Dim freq(1 To 10) As Integer
    Dim comb(1 To 7, 1 To 5) As Integer
    Dim i, j, k, l, m As Integer
    Dim count As Integer
    
    'initialize the numbers array
    numbers = Array(4, 29, 25, 21, 33, 1, 16, 31, 35, 36)
    
    'initialize the frequency array
    freq(1) = 4
    freq(2) = 4
    freq(3) = 4
    freq(4) = 4
    freq(5) = 3
    freq(6) = 3
    freq(7) = 4
    freq(8) = 3
    freq(9) = 3
    freq(10) = 3
    
    'generate the combinations
    For i = 1 To 10
        For j = i + 1 To 10
            For k = j + 1 To 10
                For l = k + 1 To 10
                    For m = l + 1 To 10
                        If freq(i) > 0 And freq(j) > 0 And freq(k) > 0 And freq(l) > 0 And freq(m) > 0 Then
                            comb(count + 1, 1) = numbers(i)
                            comb(count + 1, 2) = numbers(j)
                            comb(count + 1, 3) = numbers(k)
                            comb(count + 1, 4) = numbers(l)
                            comb(count + 1, 5) = numbers(m)
                            freq(i) = freq(i) - 1
                            freq(j) = freq(j) - 1
                            freq(k) = freq(k) - 1
                            freq(l) = freq(l) - 1
                            freq(m) = freq(m) - 1
                            count = count + 1
                            If count = 7 Then Exit Sub
                        End If
                    Next m
                Next l
            Next k
        Next j
    Next i
    
    'output the combinations
    Range("F2:J8").Value = comb
    
End Sub
error again
sometime nobody help because looks like you are doing nothing, but in this case I am really trying.
VBA Code:
Sub Genetions()
    Dim numbers(1 To 10) As Integer
    Dim freq(1 To 10) As Integer
    Dim combinations(1 To 7, 1 To 5) As Integer
    Dim i As Integer, j As Integer, k As Integer, l As Integer, m As Integer
    
    'Initialize numbers and frequency
    numbers(1) = 4: freq(1) = 4
    numbers(2) = 29: freq(2) = 4
    numbers(3) = 25: freq(3) = 4
    numbers(4) = 21: freq(4) = 4
    numbers(5) = 33: freq(5) = 3
    numbers(6) = 1: freq(6) = 3
    numbers(7) = 16: freq(7) = 4
    numbers(8) = 31: freq(8) = 3
    numbers(9) = 35: freq(9) = 3
    numbers(10) = 36: freq(10) = 3
    
    'Generate combinations
    For i = 1 To 7
        For j = 1 To 5
            'Pick a random number from remaining numbers with nonzero frequency
            Do
                k = Int(Rnd() * 10) + 1
            Loop Until freq(k) > 0
            
            'Add number to combination and decrease its frequency
            combinations(i, j) = numbers(k)
            freq(k) = freq(k) - 1
        Next j
    Next i
    
    'Output combinations to sheet
    Range("F2").Resize(7, 5).Value = combinations
    
End Sub
 
Upvote 0
Hi, it's me again.

one thing I don't get is that your numbers and frequencies are "hard-coded" or "static" (that means they are defined at the very beginning of your code and not calculated dynamically during code execution)

So my question now is what should your program actually do? Should it get numbers from the table inside the Excel Worksheet?

The problem is, each time I ask you for some information you provide me with different numbers :p

I'm giving my best to get your code to run, but therefore I need some guidance.

Could you explain your program step by step? I know, that's a lot of work, but would really help me understand what's going on there. ;)
 
Upvote 0
Hi, and thank you, I believe the best way to be clear is to visit the website with the idea, and you will see what exactly I am talking about, they don't have codes at all, but shows what I am looking for exactly.
this is the web

 
Upvote 0
The website does only provide a result, but no information on how it is gathered.
Unfortunately I haven't found a solution yet and I'm afraid I can't figure it out 😞
 
Upvote 0

Forum statistics

Threads
1,216,125
Messages
6,128,998
Members
449,480
Latest member
yesitisasport

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top