Generate 7 random out of 9 number limiting max repeated 2 or 3

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,422
Office Version
  1. 2010
Hello,</SPAN>

Here is a bit difficult random number generator task, because I need a VBA that can choose 7 random numbers out of 9, which are listed in the cells A4:A12 </SPAN></SPAN>

Create for example 52 random in the cells F4:L55 but conditions is limit max repetitions 2 or 3 as shown in the example below is it possible?</SPAN></SPAN>

Finally in the column N list the repeated numbers amount</SPAN></SPAN>

Example...

Book1
ABCDEFGHIJKLMN
1
2
3Total Numbersn1n2n3n4n5n6n7Max Repeated
4131557132
5285815612
6331557132
7452534513
8522152813
9668715142
10734141333
11826214132
12914838283
1323113433
1436483182
1513531533
1632314182
1711584362
1814524132
1953214142
2011415833
2134152122
2219929563
2349992233
2414488222
2514533562
2612121343
2721528253
2891364532
2924111333
3051723232
3126171372
3231114883
3361814313
3435588342
3512374232
3683211813
3775332452
3884351152
3982113522
4042551113
4133556853
4233272912
4334711262
4456282372
4591369193
4627448172
4763114282
4853152822
4939321113
5051919842
5125342923
5222181213
5331341582
5414332233
5521438622
Sheet10
</SPAN></SPAN>

Thank you all</SPAN></SPAN>

Excel 2000</SPAN></SPAN>
Regards,</SPAN></SPAN>
Moti</SPAN></SPAN>
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hello, just noticed in the some of rows there are 2 or 3 repeated but may be 3 numbers 2 times also it is ok also no problem </SPAN></SPAN>

Like for example in the </SPAN></SPAN>
Row 4 there is 1 a 2 times & 5 is also a 2 times</SPAN></SPAN>
Row 5 there is 1 a 2 times, 5 is also a 2 times & 8 is also a 2 times</SPAN></SPAN>
Row 6 there is 1 a 2 times, 3 is also a 2 times & 5 is also a 2 times</SPAN></SPAN>

And it could be find in other rows may be... and it is ok too</SPAN></SPAN>

Regards,</SPAN></SPAN>
Moti</SPAN></SPAN>
 
Last edited:
Upvote 0
Hello, it would be better to define in a simple way that any "among 9 num" single number cannot be appeared 4 times in the row (in other words limit for single number is 2 or 3 times max in the row) I hope may this is more clearer now</SPAN></SPAN>

Regards,</SPAN></SPAN>
Moti</SPAN></SPAN>
 
Upvote 0
Try this:-
The first Piece of code is run from "CommandButton1"
Change to a Sub if required !!!
Code:
Option Explicit
[COLOR="Navy"]Dim[/COLOR] Dic [COLOR="Navy"]As[/COLOR] Object
Private [COLOR="Navy"]Sub[/COLOR] CommandButton1_Click()
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Rw [COLOR="Navy"]As[/COLOR] Range, R [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] nRay [COLOR="Navy"]As[/COLOR] Variant, oMax [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range("F4:L55")
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Rw [COLOR="Navy"]In[/COLOR] Rng.Rows
  nRay = nRw: oMax = 0
  [COLOR="Navy"]For[/COLOR] n = 1 To UBound(nRay)
        oMax = Application.Max(Dic(nRay(n, 1)), oMax)
        [COLOR="Navy"]If[/COLOR] Dic(nRay(n, 1)) > 3 [COLOR="Navy"]Then[/COLOR]
            oMax = 0
            [COLOR="Navy"]Exit[/COLOR] For
        [COLOR="Navy"]End[/COLOR] If
   [COLOR="Navy"]Next[/COLOR] n
       [COLOR="Navy"]If[/COLOR] oMax > 0 [COLOR="Navy"]Then[/COLOR]
            Rw.Cells(1).Offset(, 8).Value = oMax
            Rw.Value = Application.Transpose(nRay)
       [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Rw

[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]


Function nRw() [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Num [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Set[/COLOR] Dic = CreateObject("scripting.dictionary")
ReDim Ray(1 To 9, 1 To 1)
    [COLOR="Navy"]For[/COLOR] Ac = 1 To 7
        Num = Application.RandBetween(1, 9)
        Ray(Ac, 1) = Num
        Dic(Num) = Dic(Num) + 1
    [COLOR="Navy"]Next[/COLOR] Ac
nRw = Ray
[COLOR="Navy"]End[/COLOR] Function

[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Here is a macro solution that you can consider...
Code:
[table="width: 500"]
[tr]
	[td]Sub RandomNumbersLimited()
  Dim R As Long, C As Long, MaxRepts As Long, HowMany As Long, Rand As Long
  Dim Nums As Variant, Result As Variant, Maxes As Variant, Used() As Long
  Randomize
  MaxRepts = 3
  HowMany = 52
  ReDim Result(1 To HowMany, 1 To 7)
  Nums = Range("A4", Cells(Rows.Count, "A").End(xlUp))
  ReDim Maxes(1 To HowMany, 1 To 1)
  For R = 1 To HowMany
    ReDim Used(1 To UBound(Nums))
    For C = 1 To 7
      Do
        Rand = Int((Nums(UBound(Nums), 1) - Nums(1, 1) + 1) * Rnd + Nums(1, 1))
        If Used(Rand) < MaxRepts Then
          Used(Rand) = Used(Rand) + 1
          Result(R, C) = Rand
          Exit Do
        End If
      Loop
    Next
    Maxes(R, 1) = Application.Max(Used)
  Next
  Range("F4:L4").Resize(UBound(Result)) = Result
  Range("N4").Resize(UBound(Maxes)) = Maxes
End Sub[/td]
[/tr]
[/table]

Edit Note: While you specifically asked for 9 total numbers to randomly distribute, my code will work if you wanted to extend that list, say, to 20 number (although you will get less repeats, of course).
 
Last edited:
Upvote 0
Try this:-
The first Piece of code is run from "CommandButton1"
Change to a Sub if required !!!
Regards Mick
Thank you Mick, code were giving an error at the line shown in red, just changed the line shown in green it worked fine, doing this I find the code generate lines with max 1 also could it be restricted Min repeated to 2, and Max repeated to 3 </SPAN></SPAN>

Code:
</SPAN></SPAN>
Changed this line</SPAN></SPAN>
[COLOR=#ff0000]Num = Application.Randbetween(1, 9) [/COLOR]</SPAN></SPAN>
 
This one </SPAN></SPAN>
[COLOR=#006400]Num = Int(Rnd * 9) + 1[/COLOR]</SPAN></SPAN>
</SPAN></SPAN>
Regards,</SPAN></SPAN>
Moti
</SPAN></SPAN>
 
Upvote 0
Here is a macro solution that you can consider...
Edit Note: While you specifically asked for 9 total numbers to randomly distribute, my code will work if you wanted to extend that list, say, to 20 number (although you will get less repeats, of course).
Thank you Rick, code worked fine!! it is generating lines with max 1 also, can be restricted Min repeated to 2, and Max repeated to 3</SPAN></SPAN>

Regards,
</SPAN></SPAN>
Moti
</SPAN></SPAN>
 
Upvote 0
Thank you Mick, code were giving an error at the line shown in red, just changed the line shown in green it worked fine, doing this I find the code generate lines with max 1 also could it be restricted Min repeated to 2, and Max repeated to 3 </SPAN></SPAN>

Code:
</SPAN></SPAN>
Changed this line</SPAN></SPAN>
[COLOR=#ff0000]Num = Application.Randbetween(1, 9) [/COLOR]</SPAN></SPAN>
 
This one </SPAN></SPAN>
[COLOR=#006400]Num = Int(Rnd * 9) + 1[/COLOR]</SPAN></SPAN>
</SPAN></SPAN>
Regards,</SPAN></SPAN>
Moti
</SPAN></SPAN>
Just so Mick knows why that line of code he used failed for you... it is because you are using Excel 2000.
 
Upvote 0
Thank you Rick, code worked fine!! it is generating lines with max 1 also, can be restricted Min repeated to 2, and Max repeated to 3</SPAN></SPAN>

Regards,
</SPAN></SPAN>
Moti
</SPAN></SPAN>
Ah, I did not understand from your original post that you wanted to force a repeat of 2 at minimum. I am not sure the method I employed will let me to that easily or not. Let me think about it and if I can modify my code to do that, I'll post it back here.
 
Upvote 0
Ah, I did not understand from your original post that you wanted to force a repeat of 2 at minimum. I am not sure the method I employed will let me to that easily or not. Let me think about it and if I can modify my code to do that, I'll post it back here.
Thank you Rick, and sorry it is my mistake, I did not clarified force a repeat of 2 at minimum. Just said Maximum repeated should be 3 </SPAN></SPAN>

Regards,
</SPAN></SPAN>
Moti
</SPAN></SPAN>
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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