DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 5.8 Simulating a Coin Flip

5.8.1 Problem

You want to simulate a coin flipping or other Boolean (true/false) event in which there is a 50% chance of either outcome.

5.8.2 Solution

Use the randRange( ) method to generate an integer that is either 0 or 1 and then correlate each possible answer with one of the desired results.

5.8.3 Discussion

You can use the randRange( ) method from Recipe 5.7 to generate a random integer in the specified range. To relate this result to an event that has two possible states, such as a coin flip (heads or tails) or a Boolean condition (true or false), treat each random integer as representing one of the possible states. By convention, programmers use 0 to represent one state (such as "off") and 1 to represent the opposite state (such as "on"), although you can use 1 and 2 if you prefer.

For example, here is how you could simulate a coin flip:

#include "Math.as"

function coinFlip (  ) {
  flip = Math.randRange(0, 1);
  if (flip == 0) {
    return "heads";
  } else {
    return "tails";
  }
}

// Example usage:
trace ("The result of the coin flip was " + coinFlip(  ));

Here, we write a function that tests our coinFlip( ) routine to see if it is reasonably even-handed. Do you expect a perfect 50/50 distribution regardless of the number of coin flips? Test it and see.

#include "Math.as"

function testCoinFlip (numFlips) {
  // We'll count how many of each result occurs. Initialize them to 0.
  var heads = 0;
  var tails = 0;

  // Repeat the process numFlips times and keep tabs on the results.
  for (var i = 0; i < numFlips; i++) {
    flip = Math.randRange(0, 1);
    if (flip == 0) {
      heads++;
    } else {
      tails++;
    }
  }
  // Display the results. Will it be 50/50?
  trace ("The result of " + numFlips + " coin flips was:");
  trace ("Heads: " + heads);
  trace ("Tails: " + tails);
}

// Example usage:
testCoinFlip(10);
testCoinFlip(100);
testCoinFlip(1000);

Applying a similar principle, here is a function that returns a random "yes" or "no" reply:

function randomReply (  ) {
  reply = Math.randRange(0, 1);
  if (reply == 0) {
    return "yes";
  } else {
    return "no";
  }
}

We can use a shortcut when creating a function that generates a Boolean (true or false) value. Here, we take advantage of the fact that the Boolean( ) method will convert the number 0 to false and the number 1 to true. In this case, using a range from 1 to 2 won't work because both 1 and 2 convert to the Boolean value true.

function trueOrFalse (  ) {
  return Boolean(Math.randRange(0, 1));
}

If you are testing the value of your random number, be sure to save the result in a variable (and test the saved result) rather than generating a new random number each time you perform the test.

The following is wrong because it generates independent random numbers in the dependent else if clauses. In some cases, none of the conditions will be true and the function will return undefined.

function guessABC (  ) {
  if (Math.randRange(1, 3) == 1) {
    return "A";
  } else if (Math.randRange(1, 3) == 2) {
    return "B";
  } else if (Math.randRange(1, 3) == 3) {
    return "C";
  }
  // We may arrive here unintentionally, due to our buggy implementation . . . in which
  // case undefined is returned.
}

This is the correct way to accomplish our goal:

function guessABC (  ) {
  // Store the random number.
  guess = Math.randRange(1, 3);
  // Now test it as many times as necessary.
  if (guess == 1) {
    return "A";
  } else if (guess == 2) {
    return "B";
  } else if (guess == 3) {
    return "C";
  }
}
    [ Team LiB ] Previous Section Next Section