Produce Random Numbers

S.H.A.D.O.

Well-known Member
Joined
Sep 6, 2005
Messages
1,915
Good evening,

I am trying to write a program that outputs 5 random numbers without repetition from 50 numbers.
The number of combinations to be produced is in worksheet "Random Numbers" and in cell "P3".
The code below works as far as the above is concerned.

Code:
Sub Random_Numbers_Generator()
Dim nDrawnMain As Long
Dim nFromMain As Long
Dim nDrawnLucky As Long
Dim nFromLucky As Long
Dim nComb As Long
Dim myMain() As Variant
Dim myLucky() As Variant

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False

nDrawnMain = 5
nFromMain = 50
nDrawnLucky = 2
nFromLucky = 9

Worksheets("Random Numbers").Select

With ActiveSheet
    Range("A1:J65536").Select
    Selection.ClearContents
    ReDim myMain(1 To nFromMain)
    ReDim myLucky(1 To nFromLucky)
    '	nDrawn = .Range("N3").Value
    '	nFrom = .Range("O3").Value
    nComb = .Range("P3").Value
End With

For j = 1 To nComb
 
    For H = 1 To nFromMain
        myMain(H) = H
    Next H
    
    For k = 1 To nDrawnMain
        Randomize
NewNumber:
        Number = Int(nFromMain * Rnd) + 1
        If myMain(Number) = "" Then
            GoTo NewNumber
        Else
            Cells(j, k) = myMain(Number)
            myMain(Number) = ""
        End If
    Next k
Next j

Range("N3").Select

Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub

What I would like to add is to output 2 random numbers without repetition from 9 numbers.
I would like to output these 2 extra numbers after it has produced the 5th number and skipped two cells to the right from the previous combination.
Both these combinations are totally seperate from each other so could possibly have the same numbers in them.

So basically, the program will produce two sets of random numbers without repetition:-
The first being 5 numbers from 50 numbers.
The second being 2 numbers from 9 numbers.
The 5 numbers will be output in cells A1:E1 and the 2 numbers will be output in cells G1:H1.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,224,605
Messages
6,179,860
Members
452,948
Latest member
UsmanAli786

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