VBA random numbers generator with required sum

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,422
Office Version
  1. 2010
VBA random numbers generator with required sum </SPAN></SPAN>

I need a VBA that can choose 7 random numbers out of 3 numbers, which are listed in the cells A4:A6 </SPAN></SPAN>

Create for example 50 random in the cells F4:L53 without repetitions (I mean 2 rows should not be the same) as per each row sum is as assigned in the column M, is it possible? (Sum can be vary from 0 to 14)</SPAN></SPAN>

In the example below some set are shown with row sum=7 </SPAN></SPAN>


Book1
ABCDEFGHIJKLM
1
2
3Total Numbersn1n2n3n4n5n6n7SUM
4021001127
5111112107
6201121207
712002117
811201027
920112017
1002110127
1101111127
1211120117
1302111207
1411220017
1510200227
1610012127
1710121117
1821111017
1912012107
2011021117
2112120107
2201112117
2302011127
2411121107
2500112217
2610210127
2711111207
2812200117
2910112117
3001121117
3112111017
3201120127
3312021107
3411111117
3521110027
3622101017
3711102117
3801112027
3902020217
4011120027
4111110217
4220211107
4310221107
4421020117
4511210207
4622100117
4711210117
4810211027
4911211107
5021201017
5122100027
5212110207
5311101217
Sheet1


Thank you all</SPAN></SPAN>

Excel 2000</SPAN></SPAN>
Regards,</SPAN></SPAN>
Moti</SPAN></SPAN>
 
Last edited:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Code:
Sub DoSomething()
    Dim WS As Worksheet
    Dim RangeOfCells As Range, RR As Range, RC As Range
    Dim I As Long
    Dim S As String
    Dim SD As Object
    Dim DoNextRow As Boolean

    Set WS = ActiveSheet
    Set SD = CreateObject("Scripting.dictionary")
    Set RangeOfCells = WS.Range("F4:L53")             'Your example range

    For Each RR In Intersect(RangeOfCells.Columns(1), RangeOfCells)
        I = 0
        S = ""
        Do
            For Each RC In Intersect(RR.EntireRow, RangeOfCells)
                RC.Value = Application.WorksheetFunction.RandBetween(0, 2)
                S = S & RC.Value
            Next RC

            DoNextRow = Not SD.exists(S)
            If DoNextRow Then
                SD.Add S, 0                           'Unique row
            End If
            I = I + 1
            S = ""
        Loop Until DoNextRow Or I > 10000             'I > 10000 is to prevent a runaway loop.
    Next RR
    Set SD = Nothing
End Sub
 
Upvote 0
Code:
Sub DoSomething()
   
 End Sub
Thank you rlv01, code stop at the line below and produce the error "438". </SPAN></SPAN>Please could you check it? </SPAN></SPAN>

Code:
RC.Value = Application.WorksheetFunction.RandBetween(0, 2)</SPAN></SPAN>

Regards,</SPAN></SPAN>
Moti</SPAN></SPAN>
 
Upvote 0
Thank you rlv01, code stop at the line below and produce the error "438". Please could you check it?

Code:
RC.Value = Application.WorksheetFunction.RandBetween(0, 2)

Regards,
Moti

Maybe EXCEL 2000 doesn't have RandBetween function?
 
Upvote 0
Also you may need to be sure the Analysis ToolPak is loaded.

T2812F1.png
 
Upvote 0
Maybe EXCEL 2000 doesn't have RandBetween function?
@ Hello Akuini, it is true thank you for the observations

Also you may need to be sure the Analysis ToolPak is loaded.
@ Hello rlv01, after Akuini message, goggling I found some suggestions under this link---> http://www.cpearson.com/excel/ATP.htm

After making the settings as shown, code did not worked, but some ware in the MrExcel I read that in the excel 2000 random RandBetween can be used without but the
"Application.WorksheetFunction" following the steps I modified the code line, as shown below
</SPAN></SPAN>

Rich (BB code):
</SPAN></SPAN>
This...</SPAN></SPAN>

RC.Value = Application.WorksheetFunction.RandBetween(0, 2)</SPAN></SPAN>



To this...</SPAN></SPAN>

RC.Value = RandBetween(0, 2)</SPAN></SPAN>
</SPAN></SPAN>

Macro worked but it generate each line with different sum from 1 to 14 how can I get
all lines with sum as per choice in the column M as shown #post 1 all lines with sum 7 only
</SPAN></SPAN>

Book1
ABCDEFGHIJKLM
1
2
3Total Numbersn1n2n3n4n5n6n7SUM
40112121210
5102110127
6210210116
710111127
810112117
920110228
1010122028
1110111116
1201122028
1320102016
1410011205
15212202211
1601111116
17222222113
1802021106
1910102116
2001102105
2121000025
2211222008
2302210218
2410002205
2520111027
2602202219
2710000001
2821211209
2911011217
30012122210
3112021028
3220101127
3300222219
3412000025
3520111128
3602010003
3722020107
3822101118
3900212005
4011021027
4111102218
4221000014
4310202027
4402100115
4511222019
4620200116
47222221011
4801221129
4921212019
5020111229
5111010014
5222110118
53122111210
Sheet1


Regards,
</SPAN>
Moti</SPAN></SPAN>
</SPAN>
 
Last edited:
Upvote 0
Try:
Rich (BB code):
Sub DoSomething()
    Dim WS As Worksheet
    Dim RangeOfCells As Range, RR As Range, RC As Range
    Dim I As Long
    Dim S As String
    Dim SD As Object
    Dim DoNextRow As Boolean


    Set WS = ActiveSheet
    Set SD = CreateObject("Scripting.dictionary")
    Set RangeOfCells = WS.Range("F4:L53")             'Your example range
Randomize
    For Each RR In Intersect(RangeOfCells.Columns(1), RangeOfCells)
        I = 0
        S = ""
        Do
            For Each RC In Intersect(RR.EntireRow, RangeOfCells)
                RC.Value = Int(Rnd() * 3)
                S = S & RC.Value
            Next RC


            DoNextRow = Not SD.exists(S)
            If DoNextRow Then
                SD.Add S, 0                           'Unique row
            End If
            I = I + 1
            S = ""
        Loop Until DoNextRow Or I > 10000             'I > 10000 is to prevent a runaway loop.
    Next RR
    Set SD = Nothing
End Sub
 
Upvote 0
Or try:
Code:
Public Sub MyRandom()
Dim arr(1 To 7)
Dim i As Long
Dim mySum As Long
Randomize
Do While mySum <> 7
mySum = 0
    For i = 1 To 7
        arr(i) = Int(Rnd() * 3)
        mySum = mySum + arr(i)
    Next
Loop
Range("A1:G1") = arr


End Sub
 
Upvote 0
Try:
Rich (BB code):
Sub DoSomething()
    Dim WS As Worksheet
    Dim RangeOfCells As Range, RR As Range, RC As Range
    Dim I As Long
    Dim S As String
    Dim SD As Object
    Dim DoNextRow As Boolean


    Set WS = ActiveSheet
    Set SD = CreateObject("Scripting.dictionary")
    Set RangeOfCells = WS.Range("F4:L53")             'Your example range
Randomize
    For Each RR In Intersect(RangeOfCells.Columns(1), RangeOfCells)
        I = 0
        S = ""
        Do
            For Each RC In Intersect(RR.EntireRow, RangeOfCells)
               RC.Value = Int(Rnd() * 3)
                S = S & RC.Value
            Next RC


            DoNextRow = Not SD.exists(S)
            If DoNextRow Then
                SD.Add S, 0                           'Unique row
            End If
            I = I + 1
            S = ""
        Loop Until DoNextRow Or I > 10000             'I > 10000 is to prevent a runaway loop.
    Next RR
    Set SD = Nothing
End Sub
Thank you Phuoc, for your help, but code is also doing the same generating all rows with different sums I want with the sum as per column M as shown in the post#1</SPAN></SPAN>

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

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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