Random Numbers and Choose

Picture of a some dice being rolled

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.

random(8) will give you a random number between 1 and 8 (might be 2, might be 8, might be whatever).

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.

show(random(6));

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.

Game show with an announcer and three doors: a blue door, a red door, and a purple door

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!

prize = random(3); with numbered doors beneath

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.

choose("Which door will you open?", "blue door", "red door", "purple door"); 
with a screen, showing how different choices give different numbers.

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.

prize = random(3); 
you = choose("Which door do you want to open?", "blue door", "red door", "purple door");

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.


prize = random(3); 
you = choose("Which door do you want to open?", "blue door", "red door", "purple door"); 
if (you == prize) { show("You win a cake!"); }
else { show("You don't win anything"); }

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

paper beats rock, rock beats scissors, scissors beats paper

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.

rock = 1, paper = 2, scissors = 3
you = choose("Make your choice", "rock", "paper", "scissors");

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


you = choose("Make your choice", "rock", "paper", "scissors");
comp  = random(3);
if (comp == 1) {show("rock");}
else if (comp == 2) {show("paper");}
else if (comp == 3) {show ("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.


you = choose("Make your choice", "rock", "paper", "scissors");
comp  = random(3);
if (comp == 1) {show("rock");}
else if (comp == 2) {show("paper");}
else if (comp == 3) {show ("scissors");}
if (comp == 1) {
  if (you == 1) {show("Draw");}
  else if (you == 2) {show("You win");}
  else if (you == 3) {show("I win");}

} else if (comp == 2) {
  if (you == 1) {show("I win");}
  else if (you == 2) {show("Draw");}
  else if (you == 3) {show("You win");}

} else if (comp == 3) {
  if (you == 1) {show("You win");}
  else if (you == 2) {show("I win");}
  else if (you == 3) {show("Draw");}
}

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.

Sample output with score

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

Programming Basics