Number Generator - No Repeats across but duplicated numerous times down

K3BAB87

New Member
Joined
Aug 12, 2016
Messages
1
I am trying to devise a spreadsheet where number generator is used.
I have 3 columns and 42 rows.
In each column, the numbers 15-28 must be used 3 times however going across, it must not be duplicated at all.
How do I do this?

19 28 19
28 17 21
20 17 22
24 20 16
24 28 22
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
... With difficulty.
If you wonder why it could be hard, consider 3 Sudoku puzzles atop each other. That's the same criteria, and not easily solved.

One not-so-random method is yo have A1=15, B1=16, C1=17, and sequence down. Probably not a "solution" :)

Another way might be to put 15-28 in cells k1-k14, then copy down twice.
Put =RAND() in columns L1-L42, then sort K:L on L. That gives a randomly-ordered column of 42.
Copy that to A1.
Re-sort. Copy that to B1
Re-sort. Copy that to C1

Then check rows for duplicates. If a failure happens, re-sort and copy again. Eventually you'll get a result.
 
Last edited:
Upvote 0
your first set of 3*14 in column E, anyone help on the other 2 sets?


Excel 2012
ABCDE
10.015057280.1619663523
20.352506250.892879917
30.42229240.8290541121
40.918346150.2034553416
50.729198190.910612822
60.889452160.3093823025
70.444823230.4113862521
80.548192220.7471531427
90.811464170.3117732928
100.643727200.4713732123
110.619134210.928048415
120.745928180.4687222222
130.239614260.4490852420
140.19355270.999694128
15280.8214781218
16250.1078993820
17240.5103791919
18150.3861782618
19190.856371020
20160.4533682317
21230.1021234018
22220.2919063124
23170.5119121815
24200.975996225
25210.338882827
26180.2667553215
27260.3827822726
28270.7064821625
29280.8020521326
30250.2505943319
31240.0520314227
32150.7331311528
33190.1045233921
34160.4755332016
35230.959886324
36220.7015311724
37170.137253622
38200.922638616
39210.1340033717
40180.926424519
41260.0533914126
42270.919354723
Sheet5
Cell Formulas
RangeFormula
A1=RAND()
B1=RANK(A1,$A$1:$A$14)+14
B15=B1
C1=RAND()
D1=RANK(C1,$C$1:$C$42)
E1=INDEX($B$1:$B$42,D1)
 
Upvote 0
Try this for Results in Columns "A,B,C"
Code:
[COLOR="Navy"]Sub[/COLOR] MG12Aug39
[COLOR="Navy"]Dim[/COLOR] n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer,[/COLOR] Num [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Dic2 [COLOR="Navy"]As[/COLOR] Object, Dic [COLOR="Navy"]As[/COLOR] Object
Randomize
[COLOR="Navy"]Set[/COLOR] Dic2 = CreateObject("scripting.dictionary")

[COLOR="Navy"]Do[/COLOR] Until n = 42
    [COLOR="Navy"]Set[/COLOR] Dic = CreateObject("scripting.dictionary")
    c = 0
      ReDim ray(1 To 3)
        [COLOR="Navy"]Do[/COLOR] Until c = 3
            Num = Int(Rnd * 14) + 15
                [COLOR="Navy"]If[/COLOR] Not Dic.exists(Num) [COLOR="Navy"]Then[/COLOR]
                    c = c + 1
                    ray(c) = Num
                    Dic.Add Num, Nothing
                [COLOR="Navy"]End[/COLOR] If
        [COLOR="Navy"]Loop[/COLOR]
[COLOR="Navy"]If[/COLOR] Not Dic2.exists(ray(1) & ray(2) & ray(3)) [COLOR="Navy"]Then[/COLOR]
    Dic2.Add ray(1) & ray(2) & ray(3), Nothing
    n = n + 1
    Cells(n, 1) = ray(1): Cells(n, 2) = ray(2): Cells(n, 3) = ray(3)
[COLOR="Navy"]End[/COLOR] If
Loop
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0
This is a rather brute force approach.

Code:
Sub test()
    Dim anArray() As Long
    Dim oneCell As Range, i As Long
    
    ReDim anArray(1 To 42)
    anArray(1) = 0
    For i = 2 To 42: anArray(i) = (anArray(i - 1) + 1) Mod 14: Next i
    
    With Range("A1:A42")
        .Value = Application.Transpose(anArray)
        .Offset(0, 5).FormulaR1C1 = "=rc1=rc2"
        .Offset(0, 6).FormulaR1C1 = "=rc2=rc3"
        .Offset(0, 7).FormulaR1C1 = "=rc1=rc3"
        .Offset(0, 8).Cells(1, 1).Formula = "=or(" & .Offset(0, 5).Address(, , , True) & ")"
        .Offset(0, 8).Cells(2, 1).Formula = "=or(" & .Offset(0, 5).Resize(, 3).Address(, , , True) & ")"
        
        Do Until .Offset(0, 8).Cells(1, 1).Value = False
            RandomizeArray anArray
            .Offset(0, 1).Value = Application.Transpose(anArray)
        Loop
        Do Until .Offset(0, 8).Cells(2, 1).Value = False
            RandomizeArray anArray
            .Offset(0, 2).Value = Application.Transpose(anArray)
        Loop
        

        With .Offset(0, 5)
            .Resize(, 4).ClearContents
            .Formula = "=RAND()"
        End With
        .Resize(, 6).Sort key1:=.Offset(0, 5), order1:=xlAscending
        .Offset(0, 5).ClearContents
        
        For Each oneCell In .Resize(, 3)
            oneCell.Value = oneCell.Value + 15
        Next oneCell
    End With
    
End Sub

Sub RandomizeArray(ByRef myArray As Variant)
    Dim i As Long, randIndex As Long
    Dim temp As Variant
    Dim Low As Long, High As Long
    Low = LBound(myArray): High = UBound(myArray)
    For i = Low To High
        randIndex = WorksheetFunction.RandBetween(Low, High)
        temp = myArray(i)
        myArray(i) = myArray(randIndex)
        myArray(randIndex) = temp
    Next i
End Sub
 
Last edited:
Upvote 0
ok, this is my humble attempt for a quite interesing problem.
the idea is hit F9 (re-calculate) until Cell M1 = 0 for the 3 set of 3x14 nos without repeat across (Cols J, K & L).

all columns copy formula from Row 1 all the way except Col B, for column B copy B1 to B14 then B15 to B42


Excel 2012
ABCDEFGHIJKLM
10.435933230.034367390.643239230.19258432FALSE198210
20.347439250.528556180.37766300.39997424FALSE221923
30.74742180.124604340.770828120.12217338FALSE283437
40.505263220.407896210.105924350.15388135FALSE271318
50.855405160.87171100.710264140.8203358FALSE202434
60.035002280.94455880.8584890.64162416FALSE1773
70.128269270.194156280.90078840.25799931FALSE26351
80.776331170.242901260.69528170.17432934FALSE152936
90.531687210.011232420.87619970.06115139FALSE26410
100.608882200.122331350.653337210.60822717FALSE272514
110.736784190.1312320.001424420.33756627FALSE221628
120.888465150.407151220.256771340.27356529FALSE1725
130.368837240.49834190.813207100.0099941FALSE162115
140.141859260.791075120.63039240.50795720FALSE15279
15230.142901300.694767180.25950430FALSE25414
16250.210221270.80408110.9747483FALSE244238
17180.027405400.37943290.66520914FALSE154020
18220.092083370.009711410.04410540FALSE213226
19160.97272540.88750850.38319625FALSE221419
20280.12911330.062578390.8011949FALSE162839
21270.98165430.468633250.19037633FALSE183842
22170.70435130.99533210.44739422FALSE242322
23210.591342160.86083380.838846FALSE251716
24200.96055260.421343270.40977123FALSE28316
25190.98860910.069248380.52828419FALSE23625
26150.133917310.295722330.976282FALSE182024
27240.9830620.337888310.30533428FALSE253711
28260.832535110.086388360.74544411FALSE191527
29230.96029370.045813400.8481375FALSE27268
30250.114806360.677799190.9273794FALSE17535
31180.019165410.078833370.9852071FALSE24332
32220.473777200.643949220.45038921FALSE28133
33160.97069450.662374200.00296642FALSE163913
34280.285971250.92430620.14263336FALSE19307
35270.564346170.724816130.58874918FALSE181040
36170.040671380.701803150.8349017FALSE201831
37210.699678140.91047330.71232312FALSE261229
38200.29461240.87703660.12673337FALSE20912
39190.155956290.419107280.7464610FALSE233617
40150.87911590.457937260.35367726FALSE21332
41240.31007230.316772320.66358615FALSE212230
42260.658189150.696812160.70449913FALSE231141
Sheet5
Cell Formulas
RangeFormula
A1=RAND()
B1=RANK(A1,$A$1:$A$14)+14
B15=B1
C1=RAND()
D1=RANK(C1,$C$1:$C$42)
E1=RAND()
F1=RANK(E1,$E$1:$E$42)
G1=RAND()
H1=RANK(G1,$G$1:$G$42)
I1=OR(J1=K1,J1=L1,K1=L1)
J1=INDEX($B$1:$B$42,D1)
K1=INDEX($F$1:$F$42,F1)
L1=INDEX($H$1:$H$42,H1)
M1=COUNTIF(I1:I42,TRUE)
 
Upvote 0
ok, this is my humble attempt for a quite interesing problem.
the idea is hit F9 (re-calculate) until Cell M1 = 0 for the 3 set of 3x14 nos without repeat across (Cols J, K & L).

all columns copy formula from Row 1 all the way except Col B, for column B copy B1 to B14 then B15 to B42

it can be done.

4-35-8-6-5

no of times to hit F9 to get results
 
Upvote 0
Interestingly, even this brute force solution is missing some functionality. The way the original problem was posed, I think you have to imagine have three bags with 42 balls (3 x 15, 3 x 16, ... 3 x 28) in each. You then draw one ball from each bag and hopefully you don't end up with any duplicates. If you do, you put the balls back in and try again. Of course, you still might end up with an unsolveable case when you get near the end (e.g. you're left with 28, 15, 28 in the three bags) so in that case you'd have to start again.

I think it should be possible, for example, to start with the following rows:

15, 16, 17
15, 18, 20
15, 28, 30

I thought of a way to do it in code but I haven't got around to writing it up.

WBD
 
Upvote 0
My brute force approach iterated 300 times before success.

my logic is similar

Put three copies of 0-14 in A

do
put 3 copies of 0-14 in B
rand re-order b
loop until different than a

do
put 3 copies of 0-14 in C
rand re-order C
loop until different than a or b

rand re-order A:C
Add 15 to everything.

The numbers of loops before success were in the 600 range, but each loop was of only one column so they were shorter than a "test and re-do everything" loop
 
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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