|
-
Jul 19th, 2003, 08:21 PM
#1
Thread Starter
New Member
Unique Random Number Sequences??
Anyone now how to, within a self contained function, generate a sequence of UNIQUE random numbers all in a given range?
Based, perhaps, around a call somthing like.....
'************************************************************************
' Paramaters
' NumOfNumbers: Number of unique random numbers to generate
' minRange: Minimum value of any returned random number
' maxRange: Maximum value of any returned random number
' Returns
' randomSequence(): Array of UNIQUE random numbers
'************************************************************************
Public Function getRandomSequence(NumOfNumbers As Integer, minRange As Long, maxRange As Long) As randomSequence()
End Function
-
Jul 20th, 2003, 02:44 PM
#2
1. You can only generate pseudo random numbers
2. What do you mean unique? Must the sequence be unique? Or the numbers in the sequence?
-
Jul 20th, 2003, 02:56 PM
#3
Thread Starter
New Member
The numbers in the sequence need to be unique
-
Jul 20th, 2003, 03:38 PM
#4
There are two algorithms that I can think of:
- max-min>>numberOfNumbers (much smaller):
- generate a list, and test each number against the previous numbers in the list, if it is not unique, try again
- else:
- generate a list of all numbers in the range min...max, randomly shuffle that sequence, and only keep the top n numbers.
-
Jul 21st, 2003, 11:12 PM
#5
Frenzied Member
The suggested syntax using a Function does not seem valid, although a close approximation could be made with a User Type variable containing an array. The following is a possible alternative.
Code:
Dim RandomValue() As Long
. . .
Public Sub RandomArrayFiller(NumOfNumbers As Integer, minRange As Long, _
maxRange As Long)
Redim RandomValue(0 to NumOfNumbers) As Long ‘Value zero for luck.
Dim TentativeValue As Long
Dim J As Integer
Dim LastRandy As Integer
Randomize ‘this could be part of Main Line initialize code.
LastRandy = 0
J = 1
TentativeValue = CLng( minRange + (maxRange - minRange)*Rnd)
Do
Select Case True
Case J > LastRandy
RandomValue(J) = TentativeValue
TentativeValue = CLng(minRange _
+ (maxRange - minRange)*Rnd)
J = 1
LastRandy = LastRandy + 1
Case TentativeValue = RandomValue(J)
TentativeValue = CLng( minRange _
+ (maxRange - minRange)*Rnd)
J = 1
Case Else
J = J + 1
End Select
Loop While LastRandy < NumberOfNumbers
End Sub
Caveat emptor: I neither compiled nor tested the above.
BTW: If you are simulating some random process, it might not be correct to eliminate duplicates.
Live long & prosper.
The Dinosaur from prehistoric era prior to computers.
Eschew obfuscation!
If a billion people believe a foolish idea, it is still a foolish idea!
VB.net 2010 Express
64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|