Generate random "set of 14" out of "3-digits"

Kishan

Well-known Member
Joined
Mar 15, 2011
Messages
1,648
Office Version
  1. 2010
Platform
  1. Windows
Using Excel 2000

Hi,

I want VBA the could generate "set of 14" out of "3-digits"

My 3-digit set is in the A2:A4 consists of "1X2"
In the cells A6:A45 filled number 1 to 40 this mean I need 40 random sets or it can be asked via input "how many set required"

First "set of 14" is generated in the B6:O6
Second "set of 14" is generated in the B7:O7 and continue till B45:O45

Example sheet...


Book1
ABCDEFGHIJKLMNOP
1My Set
21
3X
42
5GenerateC1C2C3C4C5C6C7C8C9C10C11C12C13C14
61112X11111211X1
72212X221212XX21
831112212211X122
94121211121111X1
1051212112X1X212X
1162111X12X2212X2
127X2112X1X11X222
13821121111X1X111
149X222121X2X1X21
151011X1211X111221
161121XX1122X12111
17122X2X112XX2111X
18131122X221111121
1914X112211XX1X11X
20152112X12XX21121
21161X212X21X12XX2
2217X22121X111112X
2318122X2121X1222X
241911211211111121
2520211XX2X1XX111X
2621X1X1212X1X1XX2
27221X12XXX1111112
2823X22X21111X1112
2924X1121211221X1X
302521XX2222X1X21X
312621211222122X1X
32271XX111XX21X2X1
3328212XX1X11122X2
3429222111X1112XX2
3530111X22211222X1
36311X11212122X12X
373211211X1X2X11X2
3833221X1221212122
39342XXX212X21222X
403521X22221XX1XXX
413612XX2121211212
4237XX112111X1X2X2
43381122112X2XX211
4439X2112212111111
4540112X11XX112212
46
47
48
Generate Random 1X2


Thank you in advance

Regards,
Kishan
 
Last edited:
Give this code a try (you run the RandomCharacterSets macro (it will call the RandomizeArray function as it needs to)...
Hi RickRothstein, I am getting error '438' at line below
Code:
X2count =Application.RandBetween(LowerLimit, UpperLimit)
<o:p></o:p>

Please could you check?<o:p></o:p>
<o:p></o:p>
Thank you<o:p></o:p>
<o:p></o:p>
Regards,<o:p></o:p>
Kishan
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi RickRothstein, I am getting error '438' at line below
Code:
X2count =Application.RandBetween(LowerLimit, UpperLimit)
<o:p></o:p>
Sorry, I forgot about the version of Excel that you are using. Replace the code I gave you earlier with this code and then see if it works or not...
Code:
[table="width: 500"]
[tr]
	[td]Sub RandomCharacterSets()
  Dim X As Long, Z As Long, LowerLimit As Long, UpperLimit As Long, HowManySets As Long, ColCount As Long, X2count As Long, RowSet As String
  LowerLimit = 5
  UpperLimit = 9
  HowManySets = 40
  ColCount = 14
  For X = 1 To HowManySets
    X2count = Int((UpperLimit - LowerLimit + 1) * Rnd + LowerLimit)
    RowSet = String(ColCount - X2count, "1")
    For Z = 1 To X2count
      RowSet = RowSet & Choose(Int(2 * Rnd + 1), "X", "2")
    Next
    Cells(5 + X, "A").Value = X
    Cells(5 + X, "B").Resize(, ColCount) = RandomizeArray(Split(Trim(Replace(StrConv(RowSet, vbUnicode), Chr(0), " "))))
  Next
End Sub

Function RandomizeArray(ByVal ArrayIn As Variant) As Variant
  Dim cnt As Long, RandomIndex As Long, tmp As Variant
  Randomize
  If VarType(ArrayIn) >= vbArray Then
    For cnt = UBound(ArrayIn) To LBound(ArrayIn) Step -1
      RandomIndex = Int((cnt - LBound(ArrayIn) + 1) * Rnd + LBound(ArrayIn))
      tmp = ArrayIn(RandomIndex)
      ArrayIn(RandomIndex) = ArrayIn(cnt)
      ArrayIn(cnt) = tmp
    Next
  End If
  RandomizeArray = ArrayIn
End Function[/td]
[/tr]
[/table]
 
Upvote 0
Sorry, I forgot about the version of Excel that you are using. Replace the code I gave you earlier with this code and then see if it works or not...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub RandomCharacterSets()
  Dim X As Long, Z As Long, LowerLimit As Long, UpperLimit As Long, HowManySets As Long, ColCount As Long, X2count As Long, RowSet As String
  LowerLimit = 5
  UpperLimit = 9
  HowManySets = 40
  ColCount = 14
  For X = 1 To HowManySets
    X2count = Int((UpperLimit - LowerLimit + 1) * Rnd + LowerLimit)
    RowSet = String(ColCount - X2count, "1")
    For Z = 1 To X2count
      RowSet = RowSet & Choose(Int(2 * Rnd + 1), "X", "2")
    Next
    Cells(5 + X, "A").Value = X
    Cells(5 + X, "B").Resize(, ColCount) = RandomizeArray(Split(Trim(Replace(StrConv(RowSet, vbUnicode), Chr(0), " "))))
  Next
End Sub

Function RandomizeArray(ByVal ArrayIn As Variant) As Variant
  Dim cnt As Long, RandomIndex As Long, tmp As Variant
  Randomize
  If VarType(ArrayIn) >= vbArray Then
    For cnt = UBound(ArrayIn) To LBound(ArrayIn) Step -1
      RandomIndex = Int((cnt - LBound(ArrayIn) + 1) * Rnd + LBound(ArrayIn))
      tmp = ArrayIn(RandomIndex)
      ArrayIn(RandomIndex) = ArrayIn(cnt)
      ArrayIn(cnt) = tmp
    Next
  End If
  RandomizeArray = ArrayIn
End Function
[/TD]
[/TR]
</tbody>[/TABLE]
Hi Rick Rothstein, your idea is brilliant :)

I like your code it is doing extra jobs than I request, as it can be controlled inside the code number of combinations, counts of X's & 2's, either between 5-9 or can be chosen fix 5-5.

Thank you with my bottom of heart giving a great solution

Have a nice time

Regards,
Kishan
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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