Results 1 to 5 of 5

Thread: [RESOLVED] Randoming Unique Numbers

  1. #1

    Thread Starter
    Addicted Member charmedcharmer's Avatar
    Join Date
    Sep 2003
    Posts
    211

    Resolved [RESOLVED] Randoming Unique Numbers

    Hi guyz... One quick help, ummm I want to have a set of random numbers from 0 - 69. For example I have an array Set(70) and Set(0) will be 45, Set(1) will be 32, Set(2) will be 10, Set(3) will be 65, and so on.. What I need is to fill the entire Set() with numbers from 1 to 70 all randomly positioned, and no repeating numbers. Got it?

    I tried this one with my code, It worked pretty well for 0 - 19, but when I was trying 0 - 69 im getting an endless loop with my code

    VB Code:
    1. Public Sub Randomize()
    2.    Dim aCtr As Long, bCtr As Long, nowIndex As Long, pastIndex  (TOTAL) As Long, duplicate As Boolean
    3.    
    4.    
    5.    
    6.    duplicate = True
    7.    For aCtr = 0 To (TOTAL - 1)
    8.       pastIndex(aCtr) = -1
    9.    Next
    10.  
    11.    For aCtr = 0 To TOTAL_CARDS - 1
    12.       Do While duplicate = True
    13.          nowIndex = Round(Rnd() * 20)
    14.          
    15.          
    16.          For bCtr = 0 To TOTAL - 1
    17.             If pastIndex(bCtr) <> nowIndex Then
    18.                duplicate = False
    19.             Else
    20.                duplicate = True
    21.                Exit For
    22.             End If
    23.          Next
    24.       Loop
    25.      
    26.       For bCtr = 0 To TOTAL - 1
    27.          If pastIndex(bCtr) = -1 Then
    28.             pastIndex(bCtr) = nowIndex
    29.             Exit For
    30.          End If
    31.       Next
    32.          
    33.       duplicate = True
    34.    Next
    35.    
    36.    
    37.      
    38. End Sub

    what could be wrong with my code? Have I over looped? or its just not the way its suppose to be. I havent done this randomizing exercise before..

    Need Help... Thanks for all the help....Love yah guyz
    Last edited by charmedcharmer; Oct 4th, 2004 at 05:38 AM.
    C++ Programming is overwhelming.

    Dont let it overwhelm you or you'll fall into the oblivion of its perfection

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Here is one method.

    (Note the pre loading of the Array with 70 numbers ensurs NO dups!)
    VB Code:
    1. Option Explicit
    2.  
    3. ' ***************************************************************
    4. '
    5. '               Generate 70 Random numbers (no repeats)
    6. '
    7. ' ***************************************************************
    8.  
    9.  
    10. Private Sub Form_Load()
    11. '----------------------------------------------------------------------------
    12. ' Uses an Array of 70 numbers that are subsiqently mixed randomly
    13. '----------------------------------------------------------------------------
    14.     Dim intArr(69) As Integer
    15.     Dim strBuff As String
    16.     Dim intIdx As Integer, intRnd As Integer, intBuff As Integer, intUppBound As Integer
    17.  
    18.     intUppBound = UBound(intArr)
    19.  
    20.     ' Load the Number Array with 70 numbers
    21.     For intIdx = 0 To intUppBound
    22.         intArr(intIdx) = intIdx + 1
    23.     Next
    24.  
    25.     Randomize
    26.  
    27.     ' Randomly swap Array values (some swaps may be repeated)
    28.     For intIdx = 0 To intUppBound
    29.         'Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
    30.         intRnd = Int((intUppBound - 0 + 1) * Rnd + 0)
    31.        
    32.         intBuff = intArr(intIdx)
    33.         intArr(intIdx) = intArr(intRnd)
    34.         intArr(intRnd) = intBuff
    35.     Next
    36.  
    37.     For intIdx = 0 To intUppBound
    38.         strBuff = strBuff & intArr(intIdx) & " "
    39.     Next
    40.  
    41.     MsgBox strBuff
    42.  
    43. End Sub




    Bruce.
    Last edited by Bruce Fox; Oct 3rd, 2004 at 07:01 PM.

  3. #3
    Hyperactive Member Blacknight's Avatar
    Join Date
    Nov 2002
    Posts
    381
    here:
    put this in the form:
    VB Code:
    1. Option Explicit
    2. Dim lenOA As Integer
    3. Private Sub Form_Activate()
    4. 'prints out all resaults on your form
    5. Dim i As Long
    6. For i = 0 To lenOA - 1
    7.     Me.Print setP(i)
    8. Next
    9. End Sub
    10.  
    11. Private Sub Form_Load()
    12. Randomize
    13. Dim alrd, i, y As Long
    14.  
    15. lenOA = 70 'set this to define array size
    16.  
    17. 'array is called setP and is defined in module
    18. Call DefineL(lenOA) 'defines array's size
    19. Call DefineL2(lenOA)
    20. Call DefineL3(lenOA)
    21.  
    22. For y = 0 To lenOA - 1
    23.    
    24.     'fill new array with the numbers left
    25.     alrd = 0
    26.     For i = 0 To lenOA - 1
    27.         If setp2(i) = False Then
    28.             setP3(alrd) = i
    29.             alrd = alrd + 1
    30.         End If
    31.     Next
    32.     alrd = Int(Rnd * alrd)
    33.     setP(y) = setP3(alrd)
    34.     setp2(setP3(alrd)) = True
    35. Next
    36.  
    37. '********************
    38. 'final resaults are all in setP()
    39. '********************
    40. End Sub

    and put the following in a module:
    VB Code:
    1. Public setP(), setP3() As Long
    2. Public setp2() As Boolean
    3. Public Function DefineL(lenOarray As Integer)
    4. ReDim setP(lenOarray - 1)
    5. End Function
    6. Public Function DefineL2(lenOarray As Integer)
    7. ReDim setp2(lenOarray - 1)
    8. End Function
    9. Public Function DefineL3(lenOarray As Integer)
    10. ReDim setP3(lenOarray - 1)
    11. End Function

    hope it helps

  4. #4
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    You could also use a collection to do this.
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim col As Collection
    3. Dim i As Integer
    4. Dim x As Integer
    5. Dim intRand(69) As Integer
    6.  
    7.     'Create a collection containing all the possible numbers
    8.     Set col = New Collection
    9.     For i = 0 To 69
    10.         col.Add i
    11.     Next i
    12.    
    13.     Randomize
    14.     For i = 0 To 69
    15.         'Grab a random index from the collection
    16.         x = Int(Rnd() * col.Count) + 1
    17.         'Add the item from the collection to an array
    18.         intRand(i) = col(x)
    19.         'Remove the item from the collection so it can't be used again
    20.         col.Remove x
    21.     Next i
    22.    
    23.     Set col = Nothing
    24.    
    25.     'Display the results
    26.     For i = 0 To 69
    27.         Debug.Print intRand(i)
    28.     Next i
    29. End Sub

  5. #5

    Thread Starter
    Addicted Member charmedcharmer's Avatar
    Join Date
    Sep 2003
    Posts
    211
    thanks.. i didnt thought of that.. silly me.... maybe its stupid me...
    thanks.. May the force be with yah guyz... ummm One person wrote something like this in vbforums: "When we encounter some problem in coding, we're stuck, then we go to vbforums, then somebody answers, then afterwards we'll realize its just a minor problem not worth asking over the forum cause we could have done it ourselves....." ehehehehehe


    THANKS THANKS THANKS
    C++ Programming is overwhelming.

    Dont let it overwhelm you or you'll fall into the oblivion of its perfection

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