Getting StartedGiving Commands to ObjectsPrograms 1Programs 2CalculatorVariables: Remembering StuffStrings 1Strings 2InputIfWhile: Loops and Repeating ThingsRandom Numbers and ChooseFSM: The Finite State Machine
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.

|

Programming Basics

Programming Basics: Beginners

E-mail | About | Privacy Policy