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:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Would the formula =INDEX($A$2:$A$4, RANDBETWEEN(1,3), 1) put in B6 and dragged down and right do what you want?
 
Upvote 0
Would the formula =INDEX($A$2:$A$4, RANDBETWEEN(1,3), 1) put in B6 and dragged down and right do what you want?
Hi mikerickson, yes your formula is doing the perfect job.

Thank you for your help

Regards,
Kishan

 
Upvote 0
Hi again,

Could it is possible to limit "X & 2" counts in each "set of 14" between 5 to 9, I mean do not show the sets have counts less the 5 and grater than 9 of "X & 2"

These are the formula results, which make sets, makes count of "X & 2" without limits


Book1
ABCDEFGHIJKLMNOPQ
1My Set
21
3X
42Count
5GenerateC1C2C3C4C5C6C7C8C9C10C11C12C13C14X & 2
611X12XXXX1X112X9
72X2XX2211X1112X9
8311221X2111211X6
941X2112X2X211229
105X1121XX2211X117
11622XX212221X11X10
1272X21X1211211127
1381X2X111X2112228
149X112112X2X22XX10
1510X2X222X21XX21212
161111X1XXX11111215
1712X11X21X1X1X12X8
18131X21XX221222X211
19142111X2X122221X9
20151212X212121X218
2116X22X22121X22X111
22172X11XX2X1XXX1210
2318X2X22X1XX222X213
2419X11X121211X1227
2520XX2XX11X212XX110
2621212222121X21X19
27221X1XXXXX12X22X11
28232X222121X2122X11
292421X11XXXXXX22110
3025122222XXX2X2XX13
312622X12XXXX1XX2X12
322722X2121121122X9
332821X1XX21XX1XX19
342921X12X2XX12X2X11
353011112212XX22128
3631X111XX112XX1217
373222121X21X1X2219
38332X111X12X11X228
3934X22221X21222X212
4035222XXX211211X210
413612XXX11XX2X1129
42372X1X1XXXX12X1210
433811122211XXX2218
44392122XX2XX1X2X111
454012X22XX2XXXX1212
46
47
48
Generate Random 1X2


Regards,
Kishan
 
Last edited:
Upvote 0
Hi,

Here is the example in which counts of the " X & 2" are limits between 5 to 9 may be VBA solution if it is not possible by the formula


Book1
ABCDEFGHIJKLMNOPQ
1My Set
21
3X
42Count
5GenerateC1C2C3C4C5C6C7C8C9C10C11C12C13C14X & 2
6112X12121X21X1X8
72X1X2111X1X22218
832111X1X1X2211X7
941111X21X211X1X6
105X111XX122211116
11622XXX12111121X8
127X112X222112X118
13811X11XXX12X22X9
149121122211111XX6
1510X1X112122121116
161111X1122211X11X6
17122111X2X21XXX219
18131X121X1111X1X15
1914X21221X12122118
2015111XXX1111XX1X6
211612X12121X1222X9
221721X1111X11X1X15
2318211X2X11X12X2X9
2419X12221XX2111X18
25201211X122XX2X1X9
2621111XX11211X1X15
2722X12X2111111X1X6
2823X22112112X1XXX9
29241112212X1X12217
3025XXX11XX21112X29
312612XX1X211X111X7
32271X1X2112X121116
3328121X1121X121126
3429112122112211126
353011112221XXX2X18
3631212X11X1XXX2219
37321111XX21X1X1216
38331X22X2121X11X18
3934X121111X11X1XX6
40351112212211X1227
41361212112X1X212X8
42372111X12X2212X29
4338X2112X1X11X2229
443911X1211X1112215
454021XX1122X121117
46
47
48
Generate Random 1X2-1


Regards,
Kishan
 
Last edited:
Upvote 0
Hi,

Here is the example in which counts of the " X & 2" are limits between 5 to 9 may be VBA solution if it is not possible by the formula
Is there a minimum number of X's or 2's that you would want to maintain as well?
 
Upvote 0
Is there a minimum number of X's or 2's that you would want to maintain as well?
Hi Rick Rothstein, Thank you for your reply I want random X & 2, also yes if it could be possible to maintain minimum number of X's or 2's will be the great idea.

Regards,
Kishan
 
Upvote 0
Hi Rick Rothstein, Thank you for your reply I want random X & 2, also yes if it could be possible to maintain minimum number of X's or 2's will be the great idea.
Uh, what would that minimum number be?
 
Upvote 0
Uh, what would that minimum number be?
Hi Rick Rothstein, it would be bit complicate, as I want total of X & 2 "between 5 to 9" so I need to limit for example for "X & 2" if count are 5 it could be then...
X = 0 & 2 = 5
X = 1 & 2 = 4
X = 2 & 2 = 3
X = 3 & 2 = 2
X = 1 & 2 = 4
X = 0 & 2 = 5
It would be for the "X & 2" count different pattern for 6, 7, 8 & 9 don't know how could be managed did you got any idea please suggest

Thank you

Regards,
Kishan


 
Last edited:
Upvote 0
Give this code a try (you run the RandomCharacterSets macro (it will call the RandomizeArray function as it needs to)...
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 = Application.RandBetween(LowerLimit, UpperLimit)
    RowSet = String(ColCount - X2count, "1")
    For Z = 1 To X2count
      RowSet = RowSet & Choose(Application.RandBetween(1, 2), "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]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,741
Messages
6,126,594
Members
449,320
Latest member
Antonino90

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