Results 1 to 7 of 7

Thread: Generating Random numbers, but not repeating any, is there an easy way to do this?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    WISCONSIN
    Posts
    115

    Generating Random numbers, but not repeating any, is there an easy way to do this?

    How can I generate random numbers but not repeat any that have already been generated. Is there an easy way to do this at all?
    Thanks!
    Eric

  2. #2
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Add the numbers you have generated randomly into a string, then use instr to check if that number has been generated, if so, randomly generate a new one.

    VB Code:
    1. 10
    2. x= int(rnd*255)
    3. if instr(usednums,x) > 0 then
    4. goto 10
    5. else
    6. usednums = x & "-" & usednums
    7. end if

  3. #3
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    If you know the range of numbers you want the random numbers to be generated between you can just add all those values to an array and shuffle them around like a deck of cards and then select a new random number by moving to next item in the array. For example...

    VB Code:
    1. Private Sub ShuffleArray(ByRef arr(), ByVal iShuffleTimes As Integer)
    2. Dim iCt As Integer
    3. Dim iRandElem As Integer
    4. Dim vTemp
    5.  
    6. 'assure we don't get the same set of random numbers
    7. Randomize
    8.  
    9. For iCt = 1 To iShuffleTimes
    10.    
    11.     iRandElem = (Rnd * (UBound(arr) - LBound(arr))) + LBound(arr) ' get an element within the bounds of the array randomly
    12.     vTemp = arr(LBound(arr)) ' assign the value of the first element in the array to the temporary variable
    13.     arr(LBound(arr)) = arr(iRandElem) ' shuffle the values
    14.     arr(iRandElem) = vTemp
    15.    
    16. Next iCt
    17.  
    18. End Sub
    19.  
    20.  
    21. Private Sub Form_Load()
    22.  Dim arr()
    23.  Dim i As Integer
    24.  Dim str As String
    25.  
    26.  ReDim arr(1 To 1000)
    27.  
    28.  ' load the values 1-1000 into an array
    29.  For i = 1 To 1000
    30.     arr(i) = i
    31.  Next i
    32.  
    33.  ' shuffle there positions in the array around a bit
    34.  ShuffleArray arr, 3000
    35.  
    36.  ' get the first 10 numbers, guaranteed not to be repeating
    37.  For i = 1 To 10
    38.     str = str & arr(i) & vbCrLf
    39.  Next i
    40.  
    41.  ' display the 10 random numbers
    42.  MsgBox "Here are 10 random numbers " & vbCrLf & str
    43.  
    44. End Sub

  4. #4
    Lively Member
    Join Date
    May 2005
    Posts
    125

    Re: Generating Random numbers, but not repeating any, is there an easy way to do this?

    Quote Originally Posted by DiGiTaIErRoR
    Add the numbers you have generated randomly into a string, then use instr to check if that number has been generated, if so, randomly generate a new one.

    VB Code:
    1. 10
    2. x= int(rnd*255)
    3. if instr(usednums,x) > 0 then
    4. goto 10
    5. else
    6. usednums = x & "-" & usednums
    7. end if
    i know this is old but could someone elaborate on this post. It seems to me if the variable "usednums" holds the value of the generated number and the instr() checks to see if it matches the number that was generated, wouldnt it always go back to "10" on the goto loop? thanks
    --- Science does not explain why things are what they are. What we get from Science is our interpretation of how things do what they do.
    --- No Scientific law of the universe is stable, we did not create it, and we will never understand all of its abilities.
    --- What we determine as reality is a mere assumption of what tomorrow will be based on what yesterday was.

  5. #5
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: Generating Random numbers, but not repeating any, is there an easy way to do this?

    digital error created another error here.
    say we want to randomize 20 numbers, 1-20.
    wouldn't instr(usednums,x) point to the digit 1 that is in 10 thru 19 if x=1?

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Generating Random numbers, but not repeating any, is there an easy way to do this

    In VB, a random number is between 0 and .9999, so you have to multiply it to get a range of numbers. If you check for the closest Integer, you can get a number between 0 and the highest number -1.
    VB Code:
    1. x=rnd*2
    .9999 * 2 = 1.9998
    if you say this, you get this
    Int(.9999 * 2) = 1
    and if you don't want 0, you have to add 1, so you use this:
    Int(.9999 * 2) +1 = 2

  7. #7
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Generating Random numbers, but not repeating any, is there an easy way to do this?

    eprunner06,

    Each time you generate a number add it to a collection. A collection will keep them unique.

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