Random
- math->random
- generates random numbers between zero and the upper bound exclusive
- so
x := math→random(100)
will be between 0 and 99 (inclusive) turtle→forward(x)
- non-deterministic programs, turtle will move different distance forward each time
Conditionals
- allow for branching in the execution of the program
- essentially if this then do this otherwise do this
- easiest to show this with an example
create turtle script
var x := 1 turtle.forward(100) if x = 1 then turtle.left(60) else turtle.right(60) end turtle.forward(100)
- note that there are two equal operators, one is := for creating variables, the other = tests for equality
- can change the value of x, rerun and get different behavior
- but this is not very interesting if we know what the value of x will be
need to add randomness and put in a loop
Probability
- can use random numbers, conditionals, and loops to create probability that something will happen
for example,
for 0 <= i < 100 var x := math→random(10) turtle.forward(10) if x = 0 then turtle→set pen color(colors→random) else do nothing end
- the turtle has a 1 in 10 (or 10%) chance of changing color
what if we wanted it to change more frequently, or less frequently?
Turtle State
- Can get the position of the turtle with the functions
turtle→xcor
andturtle→ycor
- Can get the dimensions of the window with
turtle→world width
andturtle→world height
- These are useful when you want to write conditionals about the turtle’s position in the window… like you will in todays activity
Quiz
- socrative.com
- Room name ‘INQ241’