Results 1 to 1 of 1

Thread: Percentages

  1. #1
    Armbruster
    Guest
    The key is going to be getting the right percentage of each number. To begin with each number (1 to 10) has a 10% percent chance of begin picked. If you set the first five numbers to 50% then each would have to exist in the "pick list" twice as much as a number with a 25% chance and so on . . .

    Here is what I came up with . . .

    VB Code:
    1. Option Explicit
    2.  
    3. Dim i As Integer
    4. Dim x As Integer
    5. Dim z As Integer
    6. Dim intNumberEntries As Integer
    7. Dim intSum As Integer
    8.  
    9. Private Sub Form_Load()
    10.     List1.Clear
    11.     List2.Clear
    12.     For i = 1 To 10
    13.         List1.AddItem i
    14.         List2.AddItem 10
    15.     Next i
    16.     Text1.Text = ""
    17.     Text2.Text = ""
    18.     Debug.Print List1.ListCount & " " & List2.ListCount
    19. End Sub
    20.  
    21. Private Sub List1_Click()
    22.     List2.Selected(List1.ListIndex) = True
    23.     Text1.Text = List2.List(List1.ListIndex)
    24.     Text1.SetFocus
    25.     Text1.SelStart = 0
    26.     Text1.SelLength = Len(Text1.Text)
    27. End Sub
    28.  
    29. Private Sub List2_Click()
    30.     List1.Selected(List2.ListIndex) = True
    31. End Sub
    32.  
    33. Private Sub Text1_Change()
    34.     If Text1.Text = "" Then Exit Sub
    35.     List2.List(List1.ListIndex) = Text1.Text
    36. End Sub
    37.  
    38. Private Sub Command1_Click()
    39.     'Here is where it gets interesting!
    40.     List3.Clear
    41.     intSum = 0
    42.     For i = 0 To List2.ListCount - 1
    43.         intSum = intSum + List2.List(i)
    44.     Next i
    45.     For i = 0 To List1.ListCount - 1
    46.         intNumberEntries = ((List2.List(i)) / 100) * intSum
    47.         For x = 1 To intNumberEntries
    48.             List3.AddItem List1.List(i)
    49.         Next x
    50.     Next i
    51.     Randomize
    52.     'now lets get that number!
    53.     z = (Rnd(1) * List3.ListCount) + 1
    54.     Text2.Text = List3.List(z)
    55. End Sub

    and here is the form . . .
    Attached Files Attached Files

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