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

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,362
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>
 
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>
I believe this modification of the code I posted earlier will do that...
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 Variant
  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
    Do
      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)
    Loop While Maxes(R, 1) = 1
  Next
  Range("F4:L4").Resize(UBound(Result)) = Result
  Range("N4").Resize(UBound(Maxes)) = Maxes
End Sub[/td]
[/tr]
[/table]



Code:
Changed this line
Num = Application.Randbetween(1, 9) 
 
This one 
Num = Int(Rnd * 9) + 1
Given the change you made to Mick's code, you will need to add a Randomize statement to his code or else you will get repeated results each time you start a new session with the workbook containing his macro.
 
Last edited:
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I believe this modification of the code I posted earlier will do that...
Code:
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 Variant
  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
    Do
      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)
    Loop While Maxes(R, 1) = 1
  Next
  Range("F4:L4").Resize(UBound(Result)) = Result
  Range("N4").Resize(UBound(Maxes)) = Maxes
End Sub

Rick, thank you for the quick alteration very appreciated. Yes it is perfect minimum 2 and the maximum 3 are looked :cool:

Given the change you made to Mick's code, you will need to add a Randomize statement to his code or else you will get repeated results each time you start a new session with the workbook containing his macro.
As I do not understand how to change Randomize statement, in Mick's code I create a new workbook to run your new code </SPAN></SPAN>

Many thanks for your help and time you spent on it to fix all
</SPAN></SPAN>

Regards,
</SPAN></SPAN>
Moti :biggrin:
</SPAN></SPAN>
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,588
Members
449,174
Latest member
chandan4057

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