Results 1 to 5 of 5

Thread: Unique Random Number Sequences??

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Posts
    2

    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

  2. #2
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    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?

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Posts
    2
    The numbers in the sequence need to be unique

  4. #4
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    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.

  5. #5
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151
    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
  •  



Click Here to Expand Forum to Full Width