Here's a slightly different approach.
Test the concept by adding a list box and a command button to a form. Then paste this code:
[tt]
Private Function UniqueRandomNumber _
(LowerBound, UpperBound, DecPlaces)
'
This function uses a string variable
'
to determine whether or not any
'
given number has already been returned.
Static Test$
'
Test for a function reset...
If LowerBound >= UpperBound Then
Test$ = ""
UniqueRandomNumber = UpperBound + 1
Exit Function
End If
LB = LowerBound + 1
Offset = Abs(1 - LB)
Range = (UpperBound + Offset) _
- (LB + Offset) + 1
Range = Range * (10 ^ DecPlaces)
If Test$ = "" Then
'
This is ether the first time the
' function has been called or the
'
function has been reset and
'
called again.
Test$ = String$(Range, 255)
End If
'
Try a few numbers to see
'
if they are unused.
'
(This will check only once
'
in ranges over 499.)
For Rep = 1 To Fix(5 / Range * 100) + 1
Start = Int(Range * Rnd + 1)
If Mid$(Test$, Start, 1) = Chr$(255) Then
Found = Start
Mid$(Test$, Found, 1) = Chr$(0)
Exit For
End If
Next
'
If that fails, pick the closest number.
If Found = 0 Then
'
Search toward the end...
Found = InStr(Start, Test$, Chr$(255))
End If
If Found = 0 Then
'
Search toward the beginning...
Found = InStrRev(Test$, Chr$(255), Start)
End If
'
If a number was found...
If Found > 0 Then
'
Mark the number as "used" and
'
return it through the function.
Mid$(Test$, Found, 1) = Chr$(0)
UniqueRandomNumber = Found _
/ (10 ^ DecPlaces) + Offset
Else
'
All numbers have been returned
'
so reset the function.
Test$ = ""
UniqueRandomNumber = UpperBound + 1
End If
End Function
Private Sub Command1_Click()
List1.Clear
'
The function returns numbers
'
greater than LowerBound and equal to
'
or less than UpperBound. In order to
'
get all numbers with two decimal places
'
from 1.24 to 9.91 you would....
LowerBound = 1.23
UpperBound = 9.91
DecPlaces = 2
Do While URI <= UpperBound
URI = UniqueRandomNumber _
(LowerBound, UpperBound, DecPlaces)
'
The function returns a number greater
'
than "UpperBound" when all numbers
'
within the range have been returned.
If URI > UpperBound Then
Exit Do
Else
'
Add the number to a list box.
List1.AddItem URI
End If
Loop
MsgBox "Click to continue...."
List1.Clear
'
If you want to retrieve less than
'
the entire list of numbers you would
'
reset the function by setting
'
LowerBound > UpperBound.
'
Then call the function once
'
for each number you require.
URI = UniqueRandomNumber(2, 1, 0)
'
Get nine unique numbers
'
between 5.2 and 20.2 (not inclusive):
LowerBound = 5.2
UpperBound = 20.1
DecPlaces = 1
For Re = 1 To 9
List1.AddItem UniqueRandomNumber _
(LowerBound, UpperBound, DecPlaces)
Next
MsgBox "Click to continue...."
List1.Clear
URI = UniqueRandomNumber(2, 1, 0)
'
Get 100 non-repeating numbers from
'
100 to 1000:
LowerBound = 99
UpperBound = 1000
DecPlaces = 0
For Re = 1 To 100
List1.AddItem UniqueRandomNumber _
(LowerBound, UpperBound, DecPlaces)
Next
End Sub
[/tt]
As written, there are no provisions for including negative numbers in a range. Including such a feature should be a fairly simple task.