Let the Computer Choose
Programs are famous for being predictable. random breaks that on purpose. Ask for a number between two limits and you get a fresh one each time, which is exactly what games, art and simulations need.
π‘ The idea
random gives a different number every time, so a program need not be the same twice.
- random 10 50 gives a whole number from 10 to 50, chosen freshly each time.
- It can go wherever a number goes: forward random 10 50, right random 0 360.
- The first number must be the smaller one.
- Narrow limits keep the drawing tidy. Wide limits make it wild.
- Run the same random program twice and you get two different pictures.
π Words to know
- random
- a number the computer picks for you
- limits
- the smallest and largest number allowed
- unpredictable
- different every time it runs
π Watch this
A wandering, wiggly path that comes out different every single time you run it.
colour rainbow width 3 repeat 24 [ forward random 10 50 right random 45 135 ]
βοΈ Your turn
- Run it three times and compare the three pictures.
- Change random 45 135 to random 80 100. The path gets much rounder.
- Change it to random 0 360 and watch the path scatter.
- Try dot random 5 30 inside the loop.
- Replace repeat 24 with repeat 60 and see how far it wanders.
π― Challenge: Make a star shower: random stars of random sizes along a random path. Run it three times and see how different each one looks. Some stars may drift off the edge, which is part of the fun.
Show me one answer
width 2 repeat 10 [ colour rainbow star random 10 35 right random 40 140 penup forward random 20 60 pendown ]
Your program