Results 1 to 19 of 19

Thread: [RESOLVED] Probability 1-20 program

Threaded View

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    35

    Resolved [RESOLVED] Probability 1-20 program



    VB Code:
    1. Public Class Form1
    2.  
    3.     'The checkList array is used to check off which numbers
    4.     'have already been randomly picked.
    5.     Private checkList(20) As Boolean
    6.     'The counter is used to show how many times it took
    7.     'for all numbers to be randomly picked.
    8.     Private counter As Integer = 0
    9.     'The track variable tells me when I should end the while....loop
    10.     'I could have just used a long if or select case, but it just
    11.     'seems easier this way.
    12.     Private track As Integer = 0
    13.     'This value of this variable (true|false) determines
    14.     'whether the While....Loop continues running or not.
    15.     Private continueLoop As Boolean = True
    16.  
    17.     'I need these variables to access the Random class
    18.     'so the program will generate a random number.
    19.     Dim generator As New Random
    20.     Dim randomValue As Integer
    21.  
    22.     Private Sub cmdRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRun.Click
    23.  
    24.         Dim i As Integer = 0
    25.         'This resets the checkList array.
    26.         For i = 0 To i < 19 Step 1
    27.             checkList(i) = False
    28.         Next i
    29.         'These three lines are just resetting the variables
    30.         'in case the program ran once before.
    31.         counter = 0
    32.         track = 0
    33.         continueLoop = True
    34.  
    35.         'The loop starts and will end when the continueLoop is set to false.
    36.         While (continueLoop)
    37.             'When the track variable reaches 19 in the array index (0-19)
    38.             'for a total of 20 indexes or more simply put 20 numbers, then
    39.             'the continueLoop will be set to false.  UPDATE:  I seen a
    40.             'mistake on my part.  The counter variable will get incremented
    41.             'by one, so the continueLoop variable is pointless, but since
    42.             'it is in there, then I'll leave it in.  I used Exit While
    43.             'so the variable counter will not get incremented.
    44.             If (track = 19) Then
    45.                 continueLoop = False
    46.                 Exit While
    47.             End If
    48.             'This static sub procedure is supposed to sync the generator variable
    49.             'with the cpu clock to help the program generate better random values,
    50.             'instead of maybe some sort of random pattern.
    51.             Randomize()
    52.             'randomValue holds a random value of (1-20).
    53.             randomValue = generator.Next(1, 20)
    54.             'Depending on what number get randomly selected, is what
    55.             'case or option will get selected.
    56.             Select Case randomValue
    57.                 Case 1
    58.                     If (checkList(0) = True) Then
    59.                         'has already been populated
    60.                         track += 1
    61.                     Else
    62.                         checkList(0) = True
    63.                     End If
    64.                 Case 2
    65.                     If (checkList(1) = True) Then
    66.                         'has already been populated
    67.                         track += 1
    68.                     Else
    69.                         checkList(1) = True
    70.                     End If
    71.                 Case 3
    72.                     If (checkList(2) = True) Then
    73.                         'has already been populated
    74.                         track += 1
    75.                     Else
    76.                         checkList(2) = True
    77.                     End If
    78.                 Case 4
    79.                     If (checkList(3) = True) Then
    80.                         'has already been populated
    81.                         track += 1
    82.                     Else
    83.                         checkList(3) = True
    84.                     End If
    85.                 Case 5
    86.                     If (checkList(4) = True) Then
    87.                         'has already been populated
    88.                         track += 1
    89.                     Else
    90.                         checkList(4) = True
    91.                     End If
    92.                 Case 6
    93.                     If (checkList(5) = True) Then
    94.                         'has already been populated
    95.                         track += 1
    96.                     Else
    97.                         checkList(5) = True
    98.                     End If
    99.                 Case 7
    100.                     If (checkList(6) = True) Then
    101.                         'has already been populated
    102.                         track += 1
    103.                     Else
    104.                         checkList(6) = True
    105.                     End If
    106.                 Case 8
    107.                     If (checkList(7) = True) Then
    108.                         'has already been populated
    109.                         track += 1
    110.                     Else
    111.                         checkList(7) = True
    112.                     End If
    113.                 Case 9
    114.                     If (checkList(8) = True) Then
    115.                         'has already been populated
    116.                         track += 1
    117.                     Else
    118.                         checkList(8) = True
    119.                     End If
    120.                 Case 10
    121.                     If (checkList(9) = True) Then
    122.                         'has already been populated
    123.                         track += 1
    124.                     Else
    125.                         checkList(9) = True
    126.                     End If
    127.                 Case 11
    128.                     If (checkList(10) = True) Then
    129.                         'has already been populated
    130.                         track += 1
    131.                     Else
    132.                         checkList(10) = True
    133.                     End If
    134.                 Case 12
    135.                     If (checkList(11) = True) Then
    136.                         'has already been populated
    137.                         track += 1
    138.                     Else
    139.                         checkList(11) = True
    140.                     End If
    141.                 Case 13
    142.                     If (checkList(12) = True) Then
    143.                         'has already been populated
    144.                         track += 1
    145.                     Else
    146.                         checkList(12) = True
    147.                     End If
    148.                 Case 14
    149.                     If (checkList(13) = True) Then
    150.                         'has already been populated
    151.                         track += 1
    152.                     Else
    153.                         checkList(13) = True
    154.                     End If
    155.                 Case 15
    156.                     If (checkList(14) = True) Then
    157.                         'has already been populated
    158.                         track += 1
    159.                     Else
    160.                         checkList(14) = True
    161.                     End If
    162.                 Case 16
    163.                     If (checkList(15) = True) Then
    164.                         'has already been populated
    165.                         track += 1
    166.                     Else
    167.                         checkList(15) = True
    168.                     End If
    169.                 Case 17
    170.                     If (checkList(16) = True) Then
    171.                         'has already been populated
    172.                         track += 1
    173.                     Else
    174.                         checkList(16) = True
    175.                     End If
    176.                 Case 18
    177.                     If (checkList(17) = True) Then
    178.                         'has already been populated
    179.                         track += 1
    180.                     Else
    181.                         checkList(17) = True
    182.                     End If
    183.                 Case 19
    184.                     If (checkList(18) = True) Then
    185.                         'has already been populated
    186.                         track += 1
    187.                     Else
    188.                         checkList(18) = True
    189.                     End If
    190.                 Case 20
    191.                     If (checkList(19) = True) Then
    192.                         'has already been populated
    193.                         track += 1
    194.                     Else
    195.                         checkList(19) = True
    196.                     End If
    197.             End Select
    198.  
    199.             counter = counter + 1
    200.         End While
    201.  
    202.         txtNumOfChances.Text = counter
    203.         txtProbability.Text = 20 / counter
    204.     End Sub
    205. End Class

    I'm not sure about this program. It seems to work, but every time I run the program to see if it get different results, it doesn't. I don't see where the mistake is at. The program does work, but with the help of the static sub procedure function Randomize(), the program still don't seem to be generating new patterns. I understand that if I close the program down, that the same pattern may get used with every executing of the program, but I'm re-running through the command button on my form.

    Anyway, maybe someone's eyes will detect what is happening and can tell me what that is by posting.

    Thanks!
    Last edited by kevin_10987; Apr 6th, 2010 at 07:49 PM.

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