Generate 1X2 combinations by percentage.

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,422
Office Version
  1. 2010
Using Excel 2010

Hello, I have a code which generate within 14 teams as per selection “Win=1”, “Draw=X” & “Loss=2” if we select all 3 options for 14 teams this will produce 3*3*3*3*3*3*3*3*3*3*3*3*3*3=4782969 combinations (3^14=4782969).

In this example I have selected 2 options for each teams in this case 2 options for 14 teams this will produce 2*2*2*2*2*2*2*2*2*2*2*2*2*2=16384 combinations (2^14=16384).

Here is macro do this job and generate using 2 double option for each 16384 combinations in the columns I:V till here hope explanations are clearer.


Here is my query each team has chosen 2 options for example team1 has X & 2 this mean there is 50% possibility of draw=X…or 50% possibility of loss=2…so far this has 50% chance for both either draw or loss. This VBA has designed to generate as seems.


What I want to be modified instead between 2 options X & 2 (50% chance can be vary that X play 20% and 2 play 80%) please see the % table in range for each team Y6:AA19…I am sure this will reduce the number of combinations. Please help with this if any question I will try to answer clearer.

Regards,
Moti

Generate 1X2 Combinations By Percentage.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAA
1
2
3TeamsFill Bettinng 1X2PredictionTeamsFill Bettinng 1X2
4TeamsWinDrawLossOptionsTeamsWinDrawLoss
5Teams1X2S, D, TS.NTeam1Team2Team3Team4Team5Team6Team7Team8Team9Team10Team11Team12Team13Team14Teams1X2
6Team1X2D1X11X111X1XX11XTeam12080
7Team21XD2X11X111X1XX112Team28020
8Team31XD3X11X111X1XX1XXTeam38020
9Team4X2D4X11X111X1XX1X2Team42080
10Team51XD5X11X111X1XXX1XTeam58020
11Team612D6X11X111X1XXX12Team68020
12Team71XD7X11X111X1XXXXXTeam78020
13Team8X2D8X11X111X1XXXX2Team88020
14Team91XD9X11X111X1X211XTeam98020
15Team10X2D10X11X111X1X2112Team102080
16Team11X2D11X11X111X1X21XXTeam112080
17Team121XD12X11X111X1X21X2Team128020
18Team131XD13X11X111X1X2X1XTeam138020
19Team14X2D14X11X111X1X2X12Team142080
2015X11X111X1X2XXX
2116X11X111X1X2XX2
22Single0117X11X111X12X11X
23Double1416.38418X11X111X12X112
24Triple0119X11X111X12X1XX
2520X11X111X12X1X2
26TT:Combi16.38421X11X111X12XX1X
2722X11X111X12XX12
2823X11X111X12XXXX
Generate 1X2 Combinations
Cell Formulas
RangeFormula
F6:F19F6=IF(COUNTA(B6:D6)=1,"S",IF(COUNTA(B6:D6)=2,"D",IF(COUNTA(B6:D6)=3,"T","")))
C22C22=COUNTIF(F6:F19,"S")
D22D22=1^C22
C23C23=COUNTIF(F6:F19,"D")
D23D23=2^C23
C24C24=COUNTIF(F6:F19,"T")
D24D24=3^C24
D26D26=D22*D23*D24


VBA Code:
Option Explicit
Sub GenerateMultiple_1X2_Combinations()

    Sheets("Generate 1X2 Combinations").Select
    Range("I6:W120000").Select
    Selection.ClearContents
    Range("G6").Select
    
Dim Ans As Boolean, Col As Collection, i As Long
Dim ColSplitted() As String
Dim ind As Integer
Set Col = New Collection
Ans = BuildLoops(BuildPreLoops, ",", Col)
If Ans Then
For i = 1 To Col.Count
ColSplitted = Split(Col(i), ",")
For ind = 0 To 13
Cells(i + 5, 9 + ind).Value = ColSplitted(ind) '(i+5=Start From Row 6)Change Output Column As desire Now it is set to 9 = I
Next ind
Next i
Else
MsgBox "Error !", vbCritical
End If
End Sub
Function BuildPreLoops() As String
Dim Rnum As Integer
Dim Cnum As Integer
BuildPreLoops = ""
For Rnum = 6 To 19
BuildPreLoops = BuildPreLoops & IIf(Cells(Rnum, 2).Text <> "", Cells(Rnum, 2).Text & IIf((Cells(Rnum, 3).Text <> "" Or Cells(Rnum, 4).Text <> ""), "+", ""), "") & _
IIf(Cells(Rnum, 3).Text <> "", Cells(Rnum, 3).Text & IIf(Cells(Rnum, 4).Text <> "", "+", ""), "") & _
IIf(Cells(Rnum, 4).Text <> "", Cells(Rnum, 4).Text, "")
BuildPreLoops = BuildPreLoops & IIf(Rnum = 19, "", ", ")
Next Rnum
End Function
Function BuildLoops(ByVal St As String, Sep As String, ByRef Col As Collection) As Boolean
Dim Ar As Variant, Ar2 As Variant, ArMain As Variant
Dim i As Long, j As Long, Ctr As Long, TempSt As String
St = Application.Substitute(St, " ", "")
Ar = Split(St, Sep)
If Not IsArray(Ar) Then Exit Function
ReDim ArMain(1 To UBound(Ar) - LBound(Ar) + 1)
For i = LBound(Ar) To UBound(Ar)
Ar2 = Split(Ar(i), "+")
Ctr = Ctr + 1
ArMain(Ctr) = Ar2
Next i
For j = LBound(ArMain(1)) To UBound(ArMain(1))
TempSt = ArMain(1)(j)
BuildString 1, ArMain, Col, TempSt
Next j
BuildLoops = True
End Function
Private Function BuildString(ByRef i As Long, ByRef ArMain As Variant, ByRef Col As Collection, ByRef TempSt As String)
Dim j As Long, St As String
St = TempSt
If i < UBound(ArMain) Then
For j = LBound(ArMain(i + 1)) To UBound(ArMain(i + 1))
TempSt = St & "," & ArMain(i + 1)(j)
BuildString i + 1, ArMain, Col, TempSt
Next j
Else
Col.Add UCase(TempSt)
End If
End Function
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hello, What I mean by % for example if I bet (1 X and 2) for any team that mean I am betting 33.33% for win, 33.33% for draw and 33.33% for in case of loss… but when I bet 1X, 12 or X2 that mean I am betting 50%, 50% for either options…and when I bet only 1 or X or 2 individual this mean 100% bet on win, draw or loss.

What I want here distribute double bet 1X, 12 or X2 instead 50%, 50%... in to 20%, 80% or 35%, 65%...for example I think team1 will win or draw so I place bet 1X…but I fell team 1 has 65% chance of winning and draw chances 35%...so programme generate for bet=1 65% combinations and for=X 35%...does it is possible?

Please help.

Regards,
Moti
 
Upvote 0
Hello, please check here is the cross post link
Regards,
Moti
 
Upvote 0
I published the answer on excelforum, as the attachment is an important part of it. (use the link in Moti post to get there).

Or you may try to re-do my steps:

It's probably not an easy task to generate a list of unique combinations fulfilling the exact requirements.

But if you want to get close to it, and you can accept for instance equal 80.43 to 19.57 chances for each team or can accept some differences between teams read on.

I used your generator first, then moved your original table with percentages for teams (and transposed it to have the same column headers Team1, Team2, ... into range: AR3:BH6

In cell W6 used this table to find what was the probablility of such result (I6) for given team :

Excel Formula:
=INDEX(AU$4:AU$6;MATCH(I6;$AT$4:$AT$6;0))
copied this formula 14 columns right (each team) and all the way down.
then calculated a sum
AK6:
Excel Formula:
=SUM(W6:AJ6)
and generated random number AL6:
Excel Formula:
=RAND()
and copied these formulas all way down too.

Then I sorted all generated and calculated columns (H:AL) descending on the sum and (ascending, but descending woold be equally good) on random number.
The topmost row (6) has now results which were 80; 80; 80 ... probable for each of Teams

I started few rows lower the last part writing in AM111
Excel Formula:
=COUNTIF(W$6:W111;W$6)/COUNT(W$6:W111)*100
it gives a percentage of more required result for team 1 in rows 6:111
copied the formula to next 13 columns (14 teams total) then calculated average and standard deviation in columns BA and BB
copied formulas in AM:BB down and obserbved results in BA and BB.
This way I found row 475 (this row will be the same if you do it from the scratch and repeat my steps) which is a good point to start adding some of less favoreable combinations from lower rows (or may be we could treat it as a final solution)?
 
Upvote 0
Kaper, first of all lot of thanks for this enormous work, since afternoon I download the workbook I generate all combinations and filled all the formula as you per your step by step instruction thank you for detailed instruction.

My mind is giving rounds since afternoon till now 6-7 hours has passed but could not pick a thread how to filter and which combinations columns to used that passes 20% or 80% also could not understand in the AL6: =RAND() what is for?

Kaper, you have good knowledge about what I want based of that you have solved this in the better way possible. Please I want to request you need your help to understand your viewpoint I am not able to find proper filtering.

Have a nice time

My best regards,
Moti :)
 
Upvote 0
Hello, I got reply from @bebo021999, if i generate 5000 sets following % is generated for each team it is closer what I want but is it possible
If within 5000 attempts, the total "2" occurrences will be 4000 times, 20% 80% as i need. here below is the result and code attached.

test2_bebo021999 v1.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAAB
1Total CombiTeamsT1T2T3T4T5T6T7T8T9T10T11T12T13T14
25.00010,00%64,04%63,54%0,00%64,90%63,72%63,56%0,00%63,94%0,00%0,00%64,24%64,34%0,00%
3TeamsFill Bettinng 1X2PredictionX36,66%35,96%36,46%36,46%35,10%0,00%36,44%63,34%36,06%36,38%36,46%35,76%35,66%36,52%TeamsFill Bettinng 1X2
4TeamsWinDrawLossOptions263,34%0,00%0,00%63,54%0,00%36,28%0,00%36,66%0,00%63,62%63,54%0,00%0,00%63,48%TeamsWinDrawLoss
5Teams1X2S, D, TS.NTeam1Team2Team3Team4Team5Team6Team7Team8Team9Team10Team11Team12Team13Team14Teams1X2
6Team1X2D12112111X1X2X12Team12080
7Team21XD2211X111XX2211XTeam28020
8Team31XD3X112111X122112Team38020
9Team4X2D4XXXX121212211XTeam42080
10Team51XD5211X11X212211XTeam58020
11Team612D621X211XX12X1X2Team68020
12Team71XD7X112X21X1X211XTeam78020
13Team8X2D8X11XX112122X12Team88020
14Team91XD92XX2121X12XX1XTeam98020
15Team10X2D102X1X111X12X112Team102080
16Team11X2D11211XX11X1X2XX2Team112080
17Team121XD122XX211XXX22X12Team128020
18Team131XD132X12X1XX12211XTeam138020
19Team14X2D1421X2111XXX21X2Team142080
20152XXXX11X12XX12
211621121212122X12
22Single011721X211XX1XX112
23Double1416.384182X12111X122112I want combinations could be generated by their selected % for any 1 X or 2 selected option as divided 33.33% for each 1X2.
24Triple011921X2111X12211X
2520211211XXX2211X
26TT:Combi16.384212112111X122XX2
27222112111212X112
2823X112X11X122112
292421121112122112
302521XX111X12X112
31262112121X12211X
322721121112X22XX2
3328X11X111X122112
342921X21112X22112
35302112111X12X112
36312112X1121XX112
3732211212XX122112
Generate 1X2 Combinations
Cell Formulas
RangeFormula
F2F2=COUNTA(I6:I16384)
I2:V4I2=IF(ISERROR(COUNTIF(I$6:I$16384,$H2)/$F$2),"",COUNTIF(I$6:I$16384,$H2)/$F$2)
F6:F19F6=IF(COUNTA(B6:D6)=1,"S",IF(COUNTA(B6:D6)=2,"D",IF(COUNTA(B6:D6)=3,"T","")))
C22C22=COUNTIF(F6:F19,"S")
D22D22=1^C22
C23C23=COUNTIF(F6:F19,"D")
D23D23=2^C23
C24C24=COUNTIF(F6:F19,"T")
D24D24=3^C24
D26D26=D22*D23*D24
Cells with Conditional Formatting
CellConditionCell FormatStop If True
I2:V4Cell Value=0textNO


VBA Code:
'https://www.excelforum.com/excel-programming-vba-macros/1424699-generate-1x2-combinations-by-selected-percentage.html#post5956185
'Generate 1X2 combinations by selected percentage.
Option Explicit
Sub RandCombi() 'By bebo021999

Range("I6:V16384").ClearContents

Dim i&, j&, k&, t&, c&, ip, st As String
Dim rng, perce(1 To 100, 1 To 14), res()
Dim dic As Object
Set dic = CreateObject("Scripting.Dictionary")
rng = Range("X6:AA19").Value
For i = 1 To 14
    k = 0
    For j = 2 To 4
        If rng(i, j) <> "" Then
            For t = 1 To rng(i, j)
                k = k + 1
                perce(k, i) = IIf(j = 2, 1, IIf(j = 3, "X", 2))
            Next
        End If
    Next
Next
ip = InputBox("How many set do you want?")
If Not IsNumeric(ip) Then
    MsgBox "Invalid number"
    Exit Sub
End If
ReDim res(1 To ip, 1 To 14)
Do
    st = ""
    For i = 1 To 14
        st = IIf(i = 1, "", st & "-") & perce(WorksheetFunction.RandBetween(1, 100), i)
    Next
    If Not dic.exists(st) Then
        c = c + 1
        dic.Add st, ""
        For i = 1 To 14
            res(c, i) = Split(st, "-")(i - 1)
        Next
    End If
Loop Until c = ip
Range("I6").Resize(ip, 14).Value = res
End Sub

Please take a look if any one can help. Thank you.

Regards,
Moti
 
Upvote 0
Do any hope? for any help to suggest solution for this complicated task % generator
 
Upvote 0
Hello, Post#6 code which generate combination very closely to % but not accurate as it should be. Please help to improve for accurate %. Thank you

Regards,
Moti :)
 
Upvote 0

Forum statistics

Threads
1,224,744
Messages
6,180,697
Members
452,994
Latest member
Janick

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