1 of 33

Create a Birthday Match Simulation

with

lynxcoding.club

2 of 33

DESCRIPTION

There are usually 365 days in a year. How many students must you have in a classroom in order to be almost certain that two students have the same birthday? 365? Half of 365? Less?

This project lets you find out by experimenting with statistics.

2

3 of 33

Description

Coding a Simulation

Students (grades 5-8) will create a computer simulation to test the probability of an event, in this case of two people in a class having the same birthday.

This activity can be modified to demonstrate the probability of other events, such as the chance of rolling the same number on a die or on multiple dice.

Students will code this app using Lynx at lynxcoding.club.

Students will code the computer to:

  • Stamp a shape for each person’s birthday in a randomly determined location.
  • Create variables using sliders, and text boxes.
  • Create conditional statements to test conditions and control action.

Prerequisite Skills - Students should know how to:

Define super- and subprocedures

Add buttons, text boxes, sliders, and turtles

Use basic turtle graphics commands

Success Criteria

Co-construct success criteria with your students but the mathematics of the simulation should work correctly.

3

4 of 33

LEARNING GOALS

Students will learn, and use, these...

4

BIG IDEAS IN CODING

MAIN IDEAS

CODE &�CONCEPTS

colourunder

Event handling and colour detection

if

Create conditional statements

settextboxname, setslidername

Change values with commands

random

Use a random number generator.

textboxname, slidername

Use text box and slider names in commands

stop, stopall

Stop single procedures or all action under program control

5 of 33

GETTING STARTED

Get a LYNX Account and Understand the Layout

5

6 of 33

Get a Lynx Account

Details at lynxcoding.club

6

NO Account

You can try Lynx for free without an account, by clicking on Create a Lynx Project on the home page at lynxcoding.club.

FREE TRIAL Account

For full access, register (click Login/Register located at the top, right side of the Lynx web page).

INDIVIDUAL Account

Convert your trial account to a permanent individual account before end of trial period.

SCHOOL ADMINISTRATOR Account

Convert your trial account to a School Administrator account before end of trial period.

Accounts are free for Canadians thanks to a subsidy by the Government of Canada.

We suggest:

• teacher gets a School Administrator Account

• students get permanent Individual Accounts

• teacher creates a ‘club’ and invites all students

7 of 33

Layout

Save

Add Objects

Files

Procedures

Clipart

Commands

Help

Share

Procedure Pane

Name your Project

Turtle

Work Area (Page)

Command Centre

Clipart Pane

My Projects

Settings

Project Tree

8 of 33

Steps for Students

The BIG Picture! (Table of Contents)

8

  1. Log in and create a new project.
  1. Add a turtle and text box.
  1. Pick a random number.
  1. Use colourunder to check dates.
  1. Write a setup procedure.
  1. Add a students slider to run the experiment.
  1. Add instructions for users.
  1. Add features.
  1. Bonus - Average (optional)

Save your work!

9 of 33

LOGIN AND GET STARTED

Log in, CREATE a NEW PROJECT,

NAME it and SAVE it.

9

10 of 33

Get Started

10

  1. Log in.

2. Click on Create a Lynx Project.

3. Name it something personal �e.g., Talia’s Birthday Match

4. Save

Click this icon

The turtle in this project is used to find “date” matches.

  • The first time it moves, it moves forward a random number from 1 to 365 and makes a small red dot, then returns to its starting point. �
  • The next time it moves, it first checks if the colour under it is red (meaning a red dot was already there). If yes, the procedure stops and "Match" is printed in a text box. If the colour under isn't red, it leaves a tiny dot and returns to the starting point.

�This process is repeated until either a match is found or there are no more students to check.

Save your work!

11 of 33

ADD A TURTLE AND TEXT BOX

Add Essential Objects

11

12 of 33

Add Essential Objects - a Turtle and a Text Box

To change the text box’s name, right-click on the text box and type Matches in the top of the dialog box. Click Apply.

12

A text box name must be one word!

  1. Set the turtle’s colour to red. Type:setc 15 Press Enter.Each colour has a number. Red’s number is 15. There is a Lynx Colour Chart in the User Guides section in the Help section at lynxcoding.club
  1. If there is no turtle in the Work area, click on the + sign and select Turtle.
  1. Make sure the turtle’s pen is up. In the Command Centre, type: pu Press Enter.

  1. Click on the + sign and select Text. Move the text box to the side of your Work Area. The Text Box will be used to keep track of the matches.

  1. Change the name of the text box to Matches.

13 of 33

TEST A RANDOM NUMBER

Explore How to Use Random

13

14 of 33

Pick a Random Number

Pick a random number from 1-365 and see the results!

Adding a 1 means you won’t get a 0 and the turtle moves forward from 1 to 365 pixels. The 1 is added before the random command.

Random picks a number from 0 to one less than its input, so 0 to 9 in this example.

14

  1. In the Command Centre, type:�show random 10�8 You probably got a different number

Try this several times. You may not always get a different number, but you won’t be able to predict what number you’ll get each time.

  1. Next, in the Command Centre, type:�forward 1 + random 365 ��Random 365 represents a random day in the year, and the turtle moves forward that number of days.��If you repeat this action many times, does the turtle always move the same amount?

Save your work!

15 of 33

CREATE EVENTS WITH COLOUR

Use Colourunder to trigger an action

15

16 of 33

Check the Dates with Colourunder

To see where the turtle moves have the turtle leave a red dot each time it moves and then return to its original position.

The sequence “pd fd 0 pu” leaves a tiny dot on the page, then the turtle returns to the centre of the work area. It is a bit “magical”, but yes, the turtle leaves a dot when it runs fd 0.

Do you get different patterns of dots each time you run the instruction?

16

  1. Move the turtle to the centre of the screen. Type:�home
  2. Then type:�repeat 10 [pu fd 1 + random 365 pd fd 0 pu home]�clean

17 of 33

A Colour Checking Procedure

This procedure checks if the colour under the turtle is colour number 15 (red).

Using stopall once there’s a match stops the all the action.

Remember: You must use a colour number with colourunder.

The next time and all the other times the turtle moves, it first needs to check if there is a colour under it. To do this, use colourunder. Colourunder checks what the colour under the turtle is.

If colourunder = 15, it means that a random birthday is the exact same date as another random birthday, therefore we have a “match”.

17

  1. Write in the Procedures Pane:�to OneStudentOneDot�pu setpos [-375 -90] ;turtle goes to this X Y coordinate�fd 1 + random 365�if colourunder = 15 [matches, pr 'MATCH!' stopall]�pd fd 0 pu ;this leaves a dot�end
  1. Type OneStudentOneDot in the Command Centre. �Try it several times, until you get a “match”!

Save your work!

18 of 33

SET UP

Set Up Your Experiment

18

19 of 33

Set Up the Turtle

Get the turtle into position

Setheading or seth sets the turtle to a specified direction in degrees.

19

  1. The turtle needs a lot of room to move. Drag it to the lower left-hand corner of the screen. Check its position. Type in the Command Centre:�show pos�-375 -90 Your numbers may be different
  1. Set the turtle’s heading to face right. Type:�setheading 90
  1. Finally, make sure there are no other marks on the screen. Type:�clean
  1. Make sure the turtle’s colour is red. Type:�setc 15

Save your work!

20 of 33

Write a Setup Procedure

Put all the instructions from the previous cards together!

20

  1. Put these instructions altogether in a Setup procedure. In the Procedures Pane, type:�to setup�clean�t1, pu�setpos [-375 -90]�setheading 90�setcolour 15�end

Use your turtle’s name followed by a comma. This ensures the turtle on this page follows your instructions. This is useful in case you decide to extend your experiment with one or more turtles later.

Save your work!

Clean will clear the graphics without moving the turtle.

21 of 33

ADD A SLIDER

Set the number of students

21

22 of 33

Add a Slider

Set how many students are in a class

22

  1. How many times should you run OneStudentOneDot? Once for each student in the class until a birthday match comes up.
  1. Right-click on the slider and name it Students.
  1. Click the + sign and select Slider A slider appears.
  1. Set the minimum to 1, the maximum to 50, and the current value to the number of students in your class.�Click Apply.
  1. Drag the slider to where you want it in the work area. Right-click on it again and select Frozen.�Click Apply.

Save your work!

23 of 33

Using the Slider

Repeat the OneStudentOneDot procedure for each student in the class

Students reports the number on the slider.

Matches followed by a comma tells the matches text box to follow the next text direction: cleartext or ct.

23

  1. Set your slider to the size of your class.
  1. In the Command Centre, type:�setup�repeat students [OneStudentOneDot]
  1. Write a procedure to run your experiment.�to OneClass�setup�matches, cleartext�repeat students [OneStudentOneDot]�end
  1. Test your procedure!

Save your work!

24 of 33

ADD USER INFORMATION

Add a button and instructions

24

25 of 33

Add a Button and Text Boxes

Help your users run the simulation.

A button’s label does not have to be one word. It’s just a label in plain English, not a command.

Drag the corner of the button to show the whole label.

Drag the button to where you want it.

Once in place, right-click on the button, select Frozen, and click Apply.

Test the button several times. How many students do you need (slider value) to get frequent matches?

25

1. Click on the + sign and select Button.�A button named “Nothing” appears.

2. Right-click on the button and give it a clear label, such as Start the Experiment.

3. Click on the arrow in the box next to �On click and select oneclass.

Save your work!

26 of 33

Add Instructions

Add one or more information text boxes

26

  1. Click on the + sign and select Text.
  1. Add instructions for the user, for example:�Set the number of students in the class
  1. Click inside the text box and use the formatting tools to write an explanation.
  1. Move the text box to where you want it.
  1. Right-click on the text box.
  1. Check Transparent and Frozen.�Click Apply.
  1. Repeat for any additional information text boxes you add.

27 of 33

Share your Project

Send it to friends or publish it!

27

  1. Click the Share icon.
  1. Click Create.�A URL will be generated here.
  1. Click on Copy Link and send it to friends.
  1. Add a Preview Image to give your project a Visual identifier.
  1. Will you allow others to copy and modify your project? Your original will remain!

28 of 33

ADVANCED CODING!

Extend Your Simulation!

28

This section teaches you how to code additional features. New procedures show how many matches there are in many classes of the same size.

It requires adding new procedures, similar to previous ones, but with important differences. Just type the procedures exactly as they are, and you will succeed!

The program you write will:

  • Use a slider to test many classes
  • Keep track of the number of classes with birthday matches
  • Calculate probability.

29 of 33

Set Up a New Page

Set up a second page for Experiment “B”

All slider, text box, turtle, and procedure names must be different from the ones you used before. In this experiment “B”, we are adding “B” to everything.

Remember, use your turtle’s name followed by a comma. This ensures the turtle on this page follows your instructions.

29

  1. Go to the + icon and select Page. Then add a turtle and a text box to the page.�Name the text box MatchesB.
  1. Add two sliders. Name one slider StudentsB and the other ClassesB.
  1. Write a setupB procedure for this page. First, check what your new turtle’s name is by right-clicking on the turtle.to setupB This must be a unique name.�clean�t2, pu setpos [-375 -90] �setheading 90�setcolour 15�end

Save your work!

30 of 33

Write a New OneClassB Procedure

Test Each Individual Class

Although similar to the Oneclass procedure, note the differences. It uses StudentsB and OneStudentOneDotB, because all these are “unique names” for your second experiment.

  • Add set to the front of the text box’s name to change the value of a text box, SetMatchesB sets the value of MatchesB.

MatchesB reports the value in the text box matchesB.

SetMatchesB MatchesB + 1 sets the number in the MatchesB text box to one more than its previous value.

30

  1. Write these three procedures in the Procedures Pane.�to OneClassB�repeat StudentsB [OneStudentOneDotB]�clean�end��to OneStudentOneDotB�t2,�pu fd 1 + random 365�if colourunder = 15 [IncreaseMatchCount]�pd fd 0 pu ;this leaves a dot�setpos [-375 -90]�end��to IncreaseMatchCount�SetMatchesB MatchesB + 1�end

Save your work!

31 of 33

Add a ManyClasses Procedure

Add a new Procedure and a Button to run it

31

  1. Add a new procedure to run oneclassB once for each class in the experiment. �to ManyClasses�setupB�SetMatchesB 0�repeat ClassesB [OneClassB]�end
  1. Add a Button. Give it a label like “Large experiment”. Select ManyClasses in the On Click. Click Apply.
  1. Add a text box with information on how to run the experiment.

Set your StudentB and ClassesB sliders to different values and test ManyClasses.

Resize your button, move it to a good location. Then right-click on the button and select Frozen. Click Apply.

Save your work!

In this example, we ran the experiment 50 times with classes of 35 students. We had a total of 56 matches, which means that in some classes, we had more than one match.

32 of 33

Calculate the Average

What Are The Chances

The procedure name must be different from the text box name.

How many students must you have in a class to have an average of “close to 1

match per class?

32

  1. Add a text box. Name it Average.
  1. Write a CalcAverage procedure in the Procedures Pane. How do you calculate the average? �In this case, it’s the number of matches that occured (MatchesB) divided by the total number of ClassesB.��to CalcAverage�average, cleartext�print 'Average number of matches for classes of this size:'�print MatchesB / ClassesB�end
  1. Add a button to run this procedure. Remember to give it a meaningful label, like Average.
  1. Always TEST your buttons and procedures!

Save your work!

33 of 33

Credits

Principal Writer……….. Susan Einhorn Contributors.…………...Alain Tougas

Elena Yakovleva

Sergei Soprunov

33

Create a Birthday Match Simulation by Code To Learn is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.