Wednesday, February 9, 2011

Visual Basic 6.0 - Session 10

Use a new function Rnd() to generate a random number.  Write an application on a new form to generate a random number between 1 and 6 to simulate the rolling of a dice.  Your form will need a large picture box with the Autosize property set to true and a command button with the following code behind it.




Run your program clicking the command button several times (until the picture box is full).  Create a link to the program from the menu form (Main) under Iterations … Post test Loops … Roll the dice.

To produce a random integer x where 0 <= x < N, use the following syntax
x=Int(Rnd*N)

The following statement produces random integers in the range from 51 to 150.
x=Int(Rnd*100+51)
 
The Randomize statement ensures that the start of each sequence of random numbers is also random.
You will notice that Visual Basic has two different syntax statements for post-test iterations:
•  Do…….Loop Until (comparisontest)
•  Do…….Loop While (comparisontest)
Both are post-test loops where the comparison test appears at the bottom of the loop meaning that the code in the loop must execute at least once.  These are both equivalent to the pseudocode syntax 
     REPEAT ……. UNTIL
 
Write the algorithm for this application in pseudocode that explains what every line in the program does.

No comments:

Post a Comment