Visual C# Guide
Random Numbers - Simulations


Re-read the page on generating random numbers in C#. This page is all about using a random number generator with data types and programming constructs to test out predictions we make for events.

For example, we tend to think of a coin toss as offering a 50:50 chance for each of the two possible outcomes, heads or tails. If we toss the coin 100 times, we would expect there to be close to the same number of heads and tails. The more times we toss the coin, the closer the two numbers should be to one another.

Coin Tossing

Let's start with the example from the introduction. We are told in Maths that the probability of getting a head is 1/2 and the probability of a tail the same, 1/2. Let's test that out.

Coin Tossing Pseudocode

Declare myRandom As New Random()
Declare numHeads As integer
Declare numTosses As integer
Declare thisToss As integer
Declare stepper As integer
numTosses ← 100
For stepper ← 1 To numTosses
   thisToss ← Generate Random (0 or 1) From myRandom
   If thisToss = 0 Then numHeads ← numHeads + 1
End For
Output "Number Of Tosses: ", numTosses
Output "Heads: ", numHeads
Output "Tails: ", numTosses - numHeads

Notice that we don't need a variable to store the number of tosses that land on tails. This value can be calculated from the other variables.

Implement this algorithm as a procedure in C#. Experiment with different values for the number of tosses. Remember for values more than 32000 or so, you will need to make sure that you use the long data type instead of the integer for everything except thisToss.

Using A Parameter

The procedure becomes more useful in our simulation if we make numTosses a parameter. That way we can call the procedure several times from the main, using different sample sizes for the simulation.

In C#, this would make the start of the procedure something like,

static void simulateCoinToss(long numTosses)

The line where numTosses is declared in the procedure should be removed.

Now you can use a loop to execute the procedure with different values for numTosses. For example,

for (long stepper = 100; stepper <= 1000000; stepper *= 10)

Comparing Expected And Actual Results

There is obviously a lot more that you can do with this once you have the core procedure for running a trial. There are lots of standard statistical measures that can be applied to this simulation. If you are serious about your Maths, look up the Math class in this guide as well as in the MSDN and seesome of the built-in functions. Also use the program to perform calculations based on the things that you have learned in Maths.

Dice Rolling

The following pseudocode describes an algorithm for simulating numRolls dice rolls and counting the number of times each number occurs. An array is used to do this. C# arrays are zero-indexed. The first subscript is 0. The array that stores how many times each number is rolled goes from 0 To 5. If we generate the random number in the same range, the algorithm is even easier. If the random number is say, a 4, we increase the value of result[4] by 1. The value of the random number matches the subscript of the array element that we want to increment.

Function simulateDiceRoll(numRolls) As Long[]
   Declare myRandom As New Random()
   Declare result As Long[6]
   Declare thisRoll As Integer
   Declare stepper As Long
   For stepper ← 1 To numRolls
      thisRoll ← Generate Random (0 to 5) From myRandom
      result[thisRoll] ← result[thisRoll] + 1
   End For
   Return result
End Function

You will need to write use a loop to output the results of each trial. Add this to the Main when you have written the function.

long[] rolls = new long[6];
rolls = simulateDiceRoll(60);
for (int i = 0; i < 6; i++)
   Console.WriteLine("{0}: {1}", i + 1, rolls[i]);

Run the trial with ever-increasing numbers and add some code to output the expected result in brackets next to the actual result (the number of rolls divided by 6).