View Full Version : Produce string of random/unique/sorted numbers
raskew 03-19-2008, 06:01 PM Reading about recent Powerball winner where the winning numbers were produced by computer. Basically, a player submits 5 unique numbers between 1 and 55.
Found this Excel response: http://www.ozgrid.com/VBA/RandomNumbers.htm -- easily converted to Access function. When called:
'Inputs: ? RandLotto(1, 55, 5)
'Output: 29 38 47 50 25
It seems to work as advertised, but I'm puzzled on several points:
(1) It doesn't use Randomize -- is that an issue?
(2) Can't see a mechanism that ensures unique numbers. (Although numerous tests have not produced a duplicate.)
(3) Would like output to be sorted (25 29 38 47 50) but can't find a solution using arrays.
Thought it was an interesting problem. Any thoughts/alternate solutions?
Best wishes -- Bob
WayneRyan 03-19-2008, 06:24 PM Hi Bob,
I think that the Application.Volatile accomplishes the same thing as Randomize.
He doesn't get duplicates because he generates the next number from the unchosen
ones and does a swap sort to move it out of the "unpicked" area.
You could sort the array at the end if you wanted.
Me personally, I'd stick with Access and stuff them in a table, that would alleviate
duplicates and let you sort them easily. Plus it would make it easier to check your
thousands of sets when your numbers were drawn.
It is a cute bit of code though.
See ya,
Wayne
WayneRyan 03-19-2008, 06:34 PM Bob,
Upon further review ...
They need to add a Randomize statement.
Every time you open the app you'll get the same set of numbers.
Wayne
raskew 03-19-2008, 06:43 PM Thanks Wayne-
You moved me off dead-center. Going to have to play with this. Although it didn't jump out at me, I'd removed Application.Volatile and inserted Randomize in the same location. As you suggested, I'll give a table approach a shot (never even thought of it).
Best wishes -- Bob
p.s. If you want to test your solution, drop me a line. I live within 25 miles of a participating state.
WayneRyan 03-19-2008, 06:47 PM Bob,
14 minutes til post-time here.
Wish me luck.
Wayne
raskew 03-19-2008, 06:58 PM Hi Wayne -
They need to add a Randomize statement.
Every time you open the app you'll get the same set of numbers.
That's curious. After removing the Application.Volatile statement, but before adding the Randomize statement, I tried calling the function 25+ times and never got the same set of numbers (which I expected).
Hmm -- did you actually try it?
Bob
raskew 03-19-2008, 07:25 PM Hey Wayne -
Help put me out of misery here:
For i = Top To Bottom + 1 Step -1
Randomize
r = Int(Rnd() * (i - Bottom + 1)) + Bottom
temp = iArr(r)
iArr(r) = iArr(i)
iArr(i) = temp
Next i
I'm assuming the unique feature lies within this code, but I just can't see it. Could you walk me through it -- real s-l-o-w.
Appreciate it, thanks.
Bob
WayneRyan 03-19-2008, 07:55 PM Hi Bob,
He's not picking a number, but a cell.
Fill cells 1-20 with the numbers 1-20.
Pick a random cell between 1 and 20(say 18).
Swap 18 and 20.
Pick a random cell between 1 and 19 (say 6)
Swap 6 and 19.
Pick a random cell between 1 and 18 (say 5)
swap 18 and 5.
Wayne
raskew 03-31-2008, 09:55 AM Hi -
Thanks to Wayne's patient explanation and an array-sorting routine posted by Roy Vidar at http://www.tek-tips.com/viewthread.cfm?qid=756905, have now got a routine that returns unique, random, sorted numbers:
Public Function RandLotto2(Bottom As Integer, Top As Integer, _
Amount As Integer) As String
'*******************************************
'Purpose: Produce x random/unique/sorted numbers
' between bottom and top.
'Sources: http://www.ozgrid.com/VBA/RandomNumbers.htm
' http://www.tek-tips.com/viewthread.cfm?qid=756905 (sort routine - Roy Vidar's post)
'Inputs: ? RandLotto2(1, 55, 5)
'Output: 1 5 11 18 44 (5 unique, random, sorted
' numbers between 1 and 55)
'*******************************************
Dim iArr As Variant
Dim i As Integer
Dim j As Integer
Dim r As Integer
Dim temp As Integer
ReDim iArr(Bottom To Top)
For i = Bottom To Top
iArr(i) = i
Next i
For i = Top To Bottom + 1 Step -1
Randomize
r = Int(Rnd() * (i - Bottom + 1)) + Bottom
temp = iArr(r)
iArr(r) = iArr(i)
iArr(i) = temp
Next i
For i = Bottom To Amount
For j = i + 1 To Amount
If iArr(i) > iArr(j) Then
temp = iArr(i)
iArr(i) = iArr(j)
iArr(j) = temp
End If
Next j
Next i
For i = Bottom To Bottom + Amount - 1
RandLotto2 = RandLotto2 & " " & iArr(i)
Next i
RandLotto2 = Trim(RandLotto2)
End Function
Bob
WayneRyan 04-01-2008, 12:48 PM Bob,
Nice job!
I'll try it on tonight's lottery.
Wayne
|
|