Unique random numbers

GorD

Well-known Member
Joined
Jan 17, 2004
Messages
1,447
Here's one for you that I can't think how to do. Does it need code?

At our golf club, each week we have a random draw for a voucher to spend in the Pro shop. I would like to generate the winning numbers via excel. The trouble being that there can be various number of winners each week depending on how many entrants ( normally 4 though) and the winning numbers must be unique.

So what I need is to have two input cells, 1) for the top limit of randbetween or how many people took part and 2) how many winners to generate.

So for example if 1) was 40 and 2) was 3

3 unique random numbers would be generated between 1 and 40.

Does this need code? How can you stop duplicates?
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try this

Code:
Option Explicit

Sub RandomNumber()

Dim Lowest              As Long
Dim Highest             As Long
Dim x                   As Long
Dim y                   As Long
Dim Total               As Long
Dim ChoiceTemp          As Long
Dim Choice()            As Long
Dim Repeat              As Boolean

    Application.ScreenUpdating = False

    Lowest = 1
    Highest = InputBox("what is the highest number?")
    Total = InputBox("How many combinations do you want")
    
ReDim Choice(1 To Total)
        
    For x = 1 To Total
a:
        Randomize
        Repeat = False
        ChoiceTemp = Int((Highest + 1 - Lowest) * Rnd + Lowest)
        For y = 1 To Total
            If Choice(y) = ChoiceTemp Then
                Repeat = True
                Else
            End If
        Next y
        If Repeat = True Then
            GoTo a:
            Else
            Choice(x) = ChoiceTemp
        End If
    Next x
    For x = 1 To Total
        Sheets("Sheet1").Range("A" & x).Value = Choice(x)
    Next
    
End Sub
 
Upvote 0
Hi Gord:

Let us have a look at ...
y040502h1.xls
ABCDE
1UpperLimitRandomNumbers(Alternate)
2403836
32633
41422
Sheet9


formula in cell C2 is ... =RANDBETWEEN(1,$B$2)
formula in cell C3 is ... =RANDBETWEEN(1,IF(RANDBETWEEN(1,$B$2)<>B2,$B$2))
and this then copied to cell C4

alternately a single formula in cell D2 ... =RANDBETWEEN(1,IF(RANDBETWEEN(1,$B$2)<>D1,$B$2))
and this is then copied to cells D3 and D4

Would this do?
 
Upvote 0
try this
Code:
Option Explicit

Sub generate_randomnumbers()
Dim entr As Integer
Dim wi As Integer
Dim nr(999) As Integer
Dim i As Integer
Dim j As Integer
Dim check As Boolean
Dim msg As String
entr = Application.InputBox("How many entrants  ?", "ENTRANTS", 1)
wi = Application.InputBox("How many winners ?", "WINNERS", 1)
Randomize Timer
For i = 1 To wi
Do
nr(i) = Int(Rnd * entr) + 1
check = False
For j = 1 To i - 1
If nr(i) = nr(j) Then check = True
Next j
Loop While check = True
msg = msg & nr(i) & Chr(10)
'or Range("A" & i) = nr(i)
Next
MsgBox msg
End Sub

I can imagine a visual approach with a table with numbers highlighted ...
regards,
Erik
 
Upvote 0
A formula approach, elaborate as Paddy intimated, that should produce distinct numbers...

A2 houses the number of entrants.

A4 is 1.

A5 is copied down:

=IF(A4<$A$2,A4+1,"")

B2 houses the desired number of distinct numbers to produce that fall between A4 and A2 inclusive.

B4 is copied down:

=IF(N(A4),RAND(),"")

C2:

=A2+CELL("Row",A4)-1

needed to establish a dynamic range that depends on A2.

C4 is copied down:

=IF(N(B4),RANK(B4,$B$4:INDEX(B:B,$C$2)),"")

E4:

=SUMPRODUCT((C4:INDEX(C:C,C2)<>"")/COUNTIF(C4:INDEX(C:C,$C$2),C4:INDEX(C:C,$C$2)))=A2

audits the uniqueness of rankings.

E5 on...

=IF($E$3*(ROW()-ROW($E$5)+1)<=$B$2,INDEX(A:A,MATCH(ROW()-ROW($E$5)+1,C:C,0)),"")

produces the list of distinct numbers whose size is given by B2.
 
Upvote 0
Hi again,
couldn't resist to make a visual approach
(the "zoom" isn't always correct: still don't know why)
play around to make coming out the results in a more "spectacular" way
Code:
Option Explicit
Sub choose_winners_visual()
Dim entr As Integer
Dim wi As Integer
Dim i As Integer
Dim nr(999) As Integer
Dim ccc As Range

entr = Application.InputBox("How many entrants  ?", "ENTRANTS", 1)
wi = Application.InputBox("How many winners ?", "WINNERS", 1)
Application.ScreenUpdating = False
Sheets.Add
With Cells
.Value = ""
.Interior.ColorIndex = 15
.ColumnWidth = 4
.RowHeight = 24
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
ActiveWindow.DisplayHeadings = False

For i = 1 To entr
Cells(Int((i - 1) / Int(Sqr(entr))) + 2, (i - 1) Mod Int(Sqr(entr)) + 2) = i
Next i
For Each ccc In Cells(2, 2).CurrentRegion
If ccc<> "" Then
ccc.Interior.ColorIndex = 14
ccc.Borders(xlEdgeLeft).LineStyle = xlContinuous
ccc.Borders(xlEdgeTop).LineStyle = xlContinuous
ccc.Borders(xlEdgeBottom).LineStyle = xlContinuous
ccc.Borders(xlEdgeRight).LineStyle = xlContinuous
End If
Next ccc
Cells(2, 2).CurrentRegion.Select
ActiveWindow.Zoom = True

Range("A1").Select
Randomize Timer
Application.ScreenUpdating = True
For i = 1 To wi
  Do
  nr(i) = Int((entr) * Rnd + 1)
  Cells.Find(What:=nr(i)).Activate
  Loop While Selection.Interior.ColorIndex = 6
  Selection.Interior.ColorIndex = 6
  Application.Wait Now + TimeValue("00:00:02")
Next i
Range("A1").Activate
End Sub

here an example
(rowheights are not displaying well here: in fact all cells are square)
kind regards,
Erik
tombola zie lotto VBA.xls
ABCDEFG
1
2123456
3789101112
4131415161718
5192021222324
6252627282930
7313233343536
837383940
Blad21
 
Upvote 0
Thanks for all the replies. I thought it could be done just couldn't think how to do it. I'll need to go and test them out now, so far tried Jacobs code which seems to work well. I'll try the rest as well.

Thanks again.
 
Upvote 0

Forum statistics

Threads
1,215,179
Messages
6,123,495
Members
449,100
Latest member
sktz

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