# 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) {
} 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) {
} else {
tails++;
}
}
// Display the results. Will it be 50/50?
trace ("The result of " + numFlips + " coin flips was:");
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";
}
}```   Preface  Part I: Local Recipes  Chapter 1. ActionScript Basics  Chapter 2. Runtime Environment  Chapter 3. Color  Chapter 4. Drawing and Masking  Chapter 5. Numbers and Math  Introduction  Recipe 5.1 Representing Numbers in Different Bases  Recipe 5.2 Converting Between Different Number Systems  Recipe 5.3 Rounding Numbers  Recipe 5.4 Inserting Leading or Trailing Zeros  Recipe 5.5 Formatting Numbers for Display  Recipe 5.6 Formatting Currency Amounts  Recipe 5.7 Generating a Random Number  Recipe 5.8 Simulating a Coin Flip  Recipe 5.9 Simulating Dice  Recipe 5.10 Simulating Playing Cards  Recipe 5.11 Generating a Unique Number  Recipe 5.12 Converting Angle Measurements  Recipe 5.13 Calculating the Distance Between Two Points  Recipe 5.14 Determining Points Along a Circle  Recipe 5.15 Converting Between Units of Measurement  Recipe 5.16 Calculating Asset Appreciation (Future Value)  Recipe 5.17 Calculating Retirement Savings  Recipe 5.18 Calculating the Loan (Mortgage) You Can Afford  Recipe 5.19 Calculating Loan Amortization or Annuities  Chapter 6. Arrays  Chapter 7. Movie Clips  Chapter 8. Text  Chapter 9. Strings  Chapter 10. Dates and Times  Chapter 11. Forms  Chapter 12. Objects and Custom Components  Chapter 13. Programming Sound  Part II: Remote Recipes  Part III: Applications  Appendix A. Unicode Escape Sequences for Latin 1 Characters  Colophon