Monday, 12 September 2011

everyday im shuffelin'

wrote pseudocode and algorithm to generate 52 random numbers in an array and display them neatly (nessesary for main project)

pseudo code: dim global variables, array items and counter
1. when command gets clicked
1.1 set counter to 0
1.2 make a collection
1.3 declare array
1.4 dimention x as integer to store random numbers
1.5 randomize
1.6 make collection a new one to start fresh
1.7 add all possible cards to collection when array item is i
1.7.1 next i
1.8 for all numbers in collection
1.8.1 make x item in collection
1.8.2 add it to array
1.8.3 display number
1.8.4 add 1 to counter
1.8.5 check counter private function
1.8.6 remove number x from collection so next is unique
1.8.7 next x
1.9. end sub
2. random generator code function
2.1 upper - lower
2.2 end function
3. check counter
3.1. when counter gets to 52
3.1.1 take new line for neater text
3.2 end if
3.3 end sub




"Option Explicit 'Force variable declaration

     Dim i As Integer 'Counter for loops
     Dim COUNTER As Integer
      Private Sub cmdstart_click()
   COUNTER = 0
          Dim Collection As Collection 'The collection we will use to store the numbers
 
          Dim cards(1 To 52) As Integer 'The array to store the values from the collection
 
      
 
          Dim X As Integer 'Variable to store the random generated number
 
        
          Randomize 'Just once to ensure that we get random values
        
          Set Collection = New Collection 'Get the collection ready to use
 
        

          For i = 1 To 52 'The possible numbers that we can have as a result is all the numbers from 1 to 52 so
              Collection.Add i 'add all the possible numbers to the collection
          Next
        

          For i = 1 To 52 'Now to get the 52 numbers we added in the previous loop
              X = RandomInteger(1, Collection.Count) 'Get a random item from the collection (that exists for sure)
              cards(i) = Collection.Item(X) 'Add it to the array
TB.Text = TB.Text & " " & cards(i) ' make space for and display each number
COUNTER = COUNTER + 1 ' add 1 to counter
checkcounter ' private sub to make results neater
              Collection.Remove X 'Remove it so we don't add it again
          Next
        

End Sub
Private Function RandomInteger(Lowerbound As Integer, Upperbound As Integer) As Integer 'The random number generator code
 
          RandomInteger = Int((Upperbound - Lowerbound + 1) * Rnd + Lowerbound)

      End Function
 
Private Sub checkcounter()
If COUNTER = 52 Then ' when all numbers picked and put in array
TB.Text = TB.Text & vbCrLf ' take new line for next set
End If

End Sub"

No comments:

Post a Comment