|
Random numbers in their original
form are not very useful in programming until weconvert them
to integers. For example, if we need to obtain a random
output of 6integers ranging from 1 to 6, which makes the
program behave as a virtual die, weneed to convert the
random numbers using the format Int(Rnd*6)+1. Let¨s study
the following example:
In this example, Int(Rnd*6) will generate a random integer
between 0 and 5 becausethe function Int truncates the
decimal part of the random number and returns aninteger.
After adding 1, you will get a random number between 1 and 6
every timeyou click the command button. For example, Let¨s
say the random number generatedis 0.98. After multiplying it
by 6, it becomes 5.88, and using the integer
functionInt(5.88) will convert the number to 5, and after
adding 1 you will get 6.In this example, you place a command
button and change its caption to `roll die¨. |
Youalso need to insert a label into the form and clear
its caption at the designing phase and
make its font bigger and bold. Then set the border
value to 1 so that it displays a border, and
after that set the alignment to center. The statement
Label1.Caption=Num means the integer
generated will be displayed as the caption of the
label. .
Dim num as integer
Private Sub Command1_Click ( )
Randomize Timer
Num=Int (Rnd*6) +1
Label1.Caption=Num
End Sub |