Results 1 to 7 of 7

Thread: Random Numbers from an array

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2000
    Posts
    4

    Cool

    I have an array with numbers 1 to 37, I want to randomly select 10 numbers from the array five times and post each number drawn to a textbox, list the 1st ten then the second, third, etc.

    Such as;
    1)2-33-21-22-5-34-11-12-31-19
    2)33-21-10-7-16-25-14-39-37-1
    30 . . .
    4) . . .
    5) . . .
    (Done)
    with no duplicates on any single line.

    Thanks in Advance!

    If you wish to email me. [email protected]

  2. #2
    Guest

    wait a minute...

    i can do what your asking but i dont get one thing, you said you dont want any duplicate numbers right? well you have 37 numbers to choose from but you are choosing 10 numbers 5 times! that makes 50 numbers out of 37, so there has to be a duplicate. i think i misunderstood you but make yourself more clear, and i am almost positive i can help.

  3. #3
    Junior Member
    Join Date
    Apr 2000
    Posts
    16

    I think this is what you might be looking for....

    I'm kind of new to this forum so I'm not sure how this code is going to show in my posting...

    You will need to create a form called Form1 with a label called Label1 - remove the Caption of this Label1 in the properties window.

    Below is the code for the following subs called Main, Run_Random, and a Function called fncAlreadySelected.

    I've tried to comment as much as possible. I'm fairly new to programming so if this doesn't do it for you (I've run it and it seems fine) there are a lot of capable people here that I'm sure could help.

    Good luck....Are you trying to simulate a lottery game of some sort? (Just curious!!)


    Code:
    Option Base 1
    Dim arrayNumbers(37) As Integer ' declare array
    Dim arrayTenNos(10) As Integer ' array for checking if random #s repeat in group of 10
    
    Sub Main()
    
        Form1.Show
        
        ' fill array
        For intList = 1 To 37
            arrayNumbers(intList) = intList
        Next intList
    
        ' run the 5 groups
        For intFiveGroups = 1 To 5
                ' calls sub that generates random number & prints to label caption
                Call Run_Random
                ' return to separate group
                Form1.Label1.Caption = Form1.Label1.Caption & Chr(13)
                ' clears contents of arrayTenNos -- sets all values = 0
                Erase arrayTenNos
        Next intFiveGroups
        
        
    End Sub
    
    Sub Run_Random()
        
        Dim intTenNumbers As Integer ' counter for 10 #s in the group
        Dim intRandom As Integer ' random #
        
        intTenNumbers = 1
        
        Do
            ' Initializes the random-number generator.
            Randomize
            ' generates random # from 1 to 37
            intRandom = Int((37) * Rnd + 1)
            ' function that evaluates whether the # is already in group
            If fncAlreadySelected(intRandom) = False Then
                ' if not in group already, assigns
                arrayTenNos(intTenNumbers) = intRandom
                ' increased groups total count
                intTenNumbers = intTenNumbers + 1
                ' post info to textbox
                If intTenNumbers > 10 Then
                    ' don't include hyphen after #
                    Form1.Label1.Caption = Form1.Label1.Caption & intRandom
                Else
                    ' do include hyphen after #
                    Form1.Label1.Caption = Form1.Label1.Caption & intRandom & "-"
                End If
            End If
            
        Loop While intTenNumbers <= 10
        
        
    End Sub
    
    Function fncAlreadySelected(intRandom As Integer)
    ' function that evaluates whether or not _
      the randomly generated # is already in the group
        
        ' loops thru arrayTerms elements
        For x = 1 To UBound(arrayTenNos)
             ' If random # is same as # already in the group, _
               set function to true, exit function
             If arrayTenNos(x) = intRandom Then
                 fncAlreadySelected = True
                 Exit Function
             End If ' If MyArray(x) > MyArray(x + 1) Then
        Next x
        
        ' loop through group and found no redundant # so set fuction to false
        fncAlreadySelected = False
    
    End Function

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2000
    Posts
    4

    hdem2

    In my question I said "with no duplicates on on SINGLE line"
    There can't be any duplicates within the first ten numbers, or the second, third, etc.

    thanks!

    Dan

  5. #5
    Guest

    oh

    ok well did the above code by mallet help you? if not then ill make a quick program and give the code.

  6. #6
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827

    Lightbulb Or alternatively...

    A more efficient way of doing it, in terms of speed, is to have 10 arrays, first one of size 37, next of size 36 etc, down to 27. Also you have the array of 10 numbers which have been picked.

    You pick an element in the first array and take the number held within that element and put it in the first position of your selected numbers array. Then you copy the other numbers into the second array, of size 36, and pick a random element etc...

    This is a little more complicated to code I suppose, so I suppose I'm just wasting your time hehe . If there was a similar situation where the algorithm might be looped many times though, you would see a difference.
    Harry.

    "From one thing, know ten thousand things."

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2000
    Posts
    4

    Cool HarryW

    If I used your process. #37 would have to select (randomly) in the first line or it would never be choosen again, and the same would go for 36 in the second array choice, 35, 34, 33. . .27

    Complicated as it would be, it wouldn't work correctly. Thanks for your reply!

    Mr.Beta

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