VBA random number generator.

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,422
Office Version
  1. 2010
Using Excel 2010
Hello,

I need help it is bit tricky random number generator…

Step1-I want first VBA create a list of numbers 1 to 54 in the range C6:C59.

Step2-Pick 1 random numbers out of list range C6:C59 and list it in cell D6.

Step3-Delete picked random from the list C6:C59 and shift up.

Step-4 repeat the Step-2 and place 2nd random below the Cell D6..and continue till find 20 random numbers…

Here below is the example sheet…showing remaining numbers in column C, 20 random in Column D, also check 54 list result in Column G.

Excel Questions.xlsm
ABCDEFGH
1
2
3
4Pick
5Numbers List 20 RandomFull ListCount
62111
73521
84631
971041
1081251
1191561
12112071
13132281
14142891
151636101
161738111
171839121
181941131
192144141
202346151
212447161
222550171
232651181
242752191
252954201
2630211
2731221
2832231
2933241
3034251
3135261
3237271
3340281
3442291
3543301
3645311
3748321
3849331
3953341
40351
41361
42371
43381
44391
45401
46411
47421
48431
49441
50451
51461
52471
53481
54491
55501
56511
57521
58531
59541
60
61
Sheet0
Cell Formulas
RangeFormula
G6:G59G6=COUNTIF(C$6:D$59,F6)


Regards,
Moti
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hello, again may be I am wrong this cannot be done. My need is to have X unique random numbers list, and also remain list of numbers too from the pool.

This was my thought may it could be done different way I will be happy to get any alternate VBA solution will be good for me.

Regards,
Moti
 
Upvote 0
Do you mean something like this?

Noteworthy.
1. Overwrites the previous result
2. Picks 20 numbers, less numbers left will cause an error
How should these points be handled?

VBA Code:
Sub TS_RandomNumbers()
Dim RandomARR(1 To 20, 1 To 1) As Integer
Dim iPick As Integer, NumbersLeft As Integer, iRAND As Integer
Dim coll As New Collection, iC As Integer
Dim NumberListRNG As Range
Dim ws As Worksheet: Set ws = ActiveSheet

Randomize

Set NumberListRNG = ws.Range("C6:C" & ws.Cells(Rows.Count, "C").End(xlUp).Row)

NumbersLeft = NumberListRNG.Cells.Count
For iC = 1 To NumbersLeft
    coll.Add NumberListRNG(iC).Value2
Next iC


For iPick = 1 To 20                     ' Draw 20 times
    iRAND = Int(NumbersLeft * Rnd) + 1  ' Random number
    RandomARR(iPick, 1) = coll(iRAND)   ' Write random number to result array
    coll.Remove (iRAND)                 ' Remove used number
    NumbersLeft = NumbersLeft - 1
Next iPick

' Clear Number List
NumberListRNG.Clear
' ReWrite Number List
Dim iRow As Integer
For iRow = 1 To coll.Count
    NumberListRNG(iRow).Value = coll(iRow)
Next

' Write return values
Range("d6:d25").Value = RandomARR ' Overwrites the previous result
End Sub

My apologies for any quirks, English is not my native language. "So I blame Google translate for all my goofs." :devilish:
 
Upvote 1
Do you mean something like this?

Noteworthy.
1. Overwrites the previous result
2. Picks 20 numbers, less numbers left will cause an error
How should these points be handled?
My apologies for any quirks, English is not my native language. "So I blame Google translate for all my goofs." :devilish:
Tupe77, I am glad it is almost perfect as I wanted.

Please I need can you add when the macro runs…..

Step1-clear the range C6:D59

Step2-creat a list in the column C6 to below 54 numbers 1 to 54.

Step3-start your macro this way never will be the less numbers left in the column C always will be 54. Also can you add if error than Exit Sub.

I appreciate your help.

Kind Regards,
Moti:)
 
Upvote 0
Is this version closer to the goal?



VBA Code:
Sub TS_RandomNumbers()
Dim RandomARR(1 To 20, 1 To 1) As Integer
Dim iPick As Integer, NumbersLeft As Integer, iRAND As Integer, iRow As Integer
Dim coll As New Collection, iC As Integer
Dim NumberListRNG As Range
On Error GoTo ErrHand

Dim ws As Worksheet: Set ws = Worksheets("Sheet0")

Randomize

Set NumberListRNG = ws.Range("C6:C59")  ' Set range

' STEP1 (just for writing all values to sheet but those are never displayed)
For iRow = 1 To 54
    NumberListRNG(iRow).Value2 = iRow   ' Create Numbers list
Next iRow

' Read Numbers list values to Collection
NumbersLeft = 54
For iC = 1 To NumbersLeft
    coll.Add NumberListRNG(iC).Value2
    ' coll.Add iC
Next iC

' Step2-Pick 1 random numbers out of list range C6:C59 and list it in cell D6. & Step3-Delete picked random from the list C6:C59 and shift up.
For iPick = 1 To 20                     ' Draw 20 times
    iRAND = Int(NumbersLeft * Rnd) + 1  ' Random number
    RandomARR(iPick, 1) = coll(iRAND)   ' Write random number to result array (Step2-Pick)
    coll.Remove (iRAND)                 ' Remove used number (Step3-Delete)
    NumbersLeft = NumbersLeft - 1
Next iPick

' Clear Number List
NumberListRNG.Clear                     ' Step3-Delete

' ReWrite Number List
For iRow = 1 To coll.Count
    NumberListRNG(iRow).Value = coll(iRow)
Next

' Write return values (Step2-Pick)
ws.Range("d6:d25").Value2 = RandomARR ' Overwrites the previous result

ErrHand: 
End Sub


My apologies for any quirks, English is not my native language. "So I blame Google translate for all my goofs." :devilish:
 
Upvote 1
Is this version closer to the goal?
My apologies for any quirks, English is not my native language. "So I blame Google translate for all my goofs." :devilish:
Tupe77, I am sorry it is my mistake :mad: I run on other sheet name, this worked superb.(y)

Also if it is not much random result in column D can be showed in ascending order.

Kind Regards,
Moti
 
Last edited:
Upvote 0
In this cleaned version, there is no first writing of the numbers 1-54 in the C column at all.
At the end, column C and D are written normally, so the end result looks the same.
Finally, the results of column D are arranged.

Would this be a suitable way to implement a number draw?

VBA Code:
Sub TS_RandomNumbers()
Dim RandomARR(1 To 20, 1 To 1) As Integer
Dim coll As New Collection, i As Integer, iRAND As Integer, NumbersLeft As Integer
Dim ws As Worksheet: Set ws = Activesheet
Dim NumberListRNG As Range: Set NumberListRNG = ws.Range("C6:C59"): NumberListRNG.Clear ' Set range

Randomize ' Create new seed

' Write Numbers list values (1 to 54) to Collection
NumbersLeft = 54            ' Index number for Collection
For i = 1 To NumbersLeft
    coll.Add i             ' Add to Collection
Next i

' Step2-Pick 20 random numbers out of list range C6:C59 and list it in cell D6:D25
For i = 1 To 20                     ' Do this 20 times
    iRAND = Int(NumbersLeft * Rnd) + 1  ' Create Random index number
    RandomARR(i, 1) = coll(iRAND)   ' Write number to result array (selected by random index number)
    coll.Remove (iRAND)                 ' Remove used number from Collection
    NumbersLeft = NumbersLeft - 1       ' Fix to Collection lenght
Next i

' Clear Number List
NumberListRNG.Clear

' ReWrite Number List
For i = 1 To coll.Count
    NumberListRNG(i).Value = coll(i)
Next

' Write return values (Step2-Pick)
ws.Range("d6:d25").Value2 = RandomARR ' Overwrites the previous result
ws.Range("d6:d25").Sort Key1:=Range("d5"), Order1:=xlAscending ' Sort range to Ascending
End Sub

My apologies for any quirks, English is not my native language. "So I blame Google translate for all my goofs." :devilish:
 
Upvote 1
Solution
The message was deleted because it was intended for another message thread.
 
Upvote 0
In this cleaned version, there is no first writing of the numbers 1-54 in the C column at all.
At the end, column C and D are written normally, so the end result looks the same.
Finally, the results of column D are arranged.
Great Tupe77, this version worked like magic. 👌 I am grateful to you being till end and solving as I wanted!

Would this be a suitable way to implement a number draw?
Tupe77, as long as my knowledge lottery draws are totally random and if random fits you are the luckiest person. Just keep trying small amount to get big price there is no more calculations :unsure:

Have a good day and Good Luck!

Kind Regards,
Moti :)
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,120
Members
453,021
Latest member
Justyna P

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