1. USING 'RND' TO SIMULATE SIMPLE PROBABILITY EXPERIMENTS


Index

Example 1a: Simulate a single toss of a fair coin.

Example 1b: Simulate a single toss of a coin with probability p of heads.

Example 1c: Simulate a single spin of a roulette wheel.

Problem 1: Simulate the throw of a fair die.

Return to List of Visual Basic Examples


Example 1a. Simulate a single toss of a fair coin.

Object, Property and settings:

Object		Property 	Setting
Form		Name            frmCoinTossFair		
		Caption		Simple Coin Toss Label
Label		Name            Label1                
		Caption		Outcome:
                Alignment	1-Right Justify Label           
Label		Name            lblOutput
                Caption		(Blank)
CommandButton	Name            cmdToss
		Caption		&Toss Coin
CommandButton	Name            cmdQuit		
		Caption         &Quit 

Event Procedures:

' In (Declarations) section of (General)

' Project Name:        Single Toss of a Fair Coin
' Date:                May, 1996
' Programmer:          R. Berman
' Description:         This program simulates a single toss 
'                      of a fair coin and outputs the result.

' Require explicit declaration of variables used in this module.
Option Explicit

Sub cmdToss_Click ()

   Randomize Timer                ' Seed random number generator with present time
                                  ' RND returns a random number, 0 <= RND < 1
   If RND < .5 Then               ' RND < .5 corresponds to heads "H"
      lblOutput.Caption = "H"     ' Display "H" in lblOutput
   Else                           ' Rnd >= .5 corresponds to tails "T"
      lblOutput.Caption = "T"     ' Display "T" in lblOutput
   End If

End 

Sub cmdQuit_Click ()

   End                            ' End the application

End Sub


Example 1b. Simulate a single toss of a coin having probability p of heads, where p is any number between 0 and 1.

Object, Property and settings:

Object		Property	Setting
Form		Name		frmCoinTossGeneral
		Caption		Simple Toss of a Coin
Label		Name		Label1
		Caption		Input the probability of heads:
		Alignment	1-Right Justify
Text		Name		txtProb
		Text		.5
Label		Name		Label2
		Caption		Outcome:
		Alignment	1-Right Justify
Label		Name		lblOutput
		Caption		(Blank)
CommandButton	Name		cmdToss
		Caption		&Toss Coin
CommandButton	Name		cmdQuit
		Caption		&Quit

Event Procedures:

' In (Declarations) section of (General)

' Project Name:        	Single Toss of a Coin
' Date:                	May, 1996
' Programmer:          	R. Berman
' Description:         	This program simulates a single toss of a coin with
'			probability input by the user.

' Require explicit declaration of variables used in this module.
Option Explicit

Sub cmdToss_Click ()

   Dim s_Prob As Single 		' Declare the variable for the user input of
					' of the probability. The "s" is a convention
					' to remind the programmer that the variable is
					' "Single" data type.
   s_Prob = Val(txtProb.Text)           ' Set s_Prob to the numerical value in txtProb.Text	
   Randomize Timer                	' Seed random number generator with present time
	 	                        ' RND returns a random number, 0 <= RND < 1
   If RND < s_Prob Then                 ' RND < s_Prob corresponds to heads
     lblOutput.Caption = "H"      	' Display "H" in lblOutput
   Else                           	' Rnd >= p corresponds to tails
     lblOutput.Caption = "T"  		' Display "T" in lblOutput
   End If

End Sub

Sub cmdQuit_Click () is the same as before.


Example 1c. Simulate a single Spin of a Roulette Wheel

Object, Property and settings:

Object		Property	Setting
Form		Name		frmRoulette
		Caption		Spin of a Roulette Wheel:
Label		Name		Label1
		Caption		The number on the roulette wheel
		Alignment	1-Right Justify
Label		Name		lblOutput
		Caption		(Blank)
CommandButton	Name		cmdSpin
		Caption		&Spin Wheel
CommandButton	Name		cmdQuit
		Caption		&Quit

Event Procedures:

' In (Declarations) section of (General)

' Project Name:        	Single Spin of a Roulette Wheel
' Date:                	May, 1996
' Programmer:          	R. Berman
' Description:         	Smulates a single spin of the roulette wheel

' Require explicit declaration of variables used in this module.
Option Explicit

Sub cmdSpin_Click ()

   Dim i_Outcome as Integer       ' Declare the integer variable i_Outcome
   Randomize Timer                ' Seed random number generator with present time
   i_Outcome = Int(38 * Rnd) - 1  ' Rnd returns a random number, 0 <= Rnd < 1
                                  ' Int(x) returns the largest integer <= x
                                  ' Therefore, i_Outcome is a random integer, -1 <= i_Outcome < 38
   IF i_Outcome <> -1 THEN        ' In case i_Outcome >=0 display its value as the outcome
     lblOutcome.Caption = Str$(i_Outcome)
   Else                           ' In case i_Outcome = -1 display "00"
     lblOutcome.Caption = "00"
   End If

End Sub

Sub cmdQuit_Click () is the same as before.


Problem 1. Simulate a single throw of a fair die giving as output the number that is showing on the face of the die.


Return to List of Visual Basic Examples