Random Numbers and Choose |

When you roll a die, you aren't sure what number you will get. You might get a one or a six or a five. This sort of number is called a *random number.*

JavaScript has a command called "random" for making random numbers. With the random command, you must give a number between the brackets. JavaScript will then give you a random number between 1 and the number you gave.

In the program area on the left, type "show(random(6));" When you click the Run button, what number is shown? If you run the program several times, you should get lots of different numbers.

Let's write a program for a game show. On this game show, there are three doors: a blue door, a red door, and a purple door. There is a prize behind one of the doors. You can choose to open one door. If the prize is there, you win it!

First, we'll number the doors. The blue door is door 1. The red door is door 2. The purple door is door 3. We can then use random(3) to choose which door has the prize. Remember that random(3) will find a number between 1 and 3, and each door is numbered either 1, 2, or 3.

The program should then ask which door you want to open: the red door, the blue door, or the purple door. We'll use a special command called "choose." With the choose command, you first give a string with some instructions. Then, you put a comma and some more strings with different things that someone can choose. If you take the first string, choose will give you back a one. If you take the second string, choose will give you back a two. If you choose the third string, choose will give you back a three. And so forth.

So the program should ask you to choose a door. We stick the choice in a variable called "you." If "you" has 1, it means the blue door. If "you" has 2, it means the red door. And if "you" has 3, it means purple door.

At the end, the program checks whether the door is the one with the prize behind it. If you opened the door with a prize, the program tells you what the prize is. Try the program out.

Extra Credit

You now know enough to write a more complicated game like Rock, Paper, and Scissors. In this game, both you and the computer must choose between rock, paper, scissors. If your choice beats the computer's choice, then you win.

The program should start by asking you to choose rock, paper, or scissors.

Then, the computer should choose rock, paper, or scissors. It uses the random() command to find a number between 1 and 3. It puts that number into the variable "comp." It then tells you what its choice was. Remember that 1 means rock, 2 means paper, and 3 means scissors.

At the end, the program needs to figure out who won. It looks at your choice and the computer's choice to see who made the better choice. If "comp" is 3 and "you" is 3, then both you and the computer chose scissors. That means nobody won, and the program will show the word draw. If "comp" is 1 and "you" is 2, then the computer chose rock and you chose paper. That means you win.

Can you change the program so that you have to play the game three times, and the computer keeps a score?