Star Student Builder logoStar Student Builder
← Code Makers Lesson 4Std 6-8

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 ]
What this program draws
What it draws

✍️ Your turn

  1. Run it three times and compare the three pictures.
  2. Change random 45 135 to random 80 100. The path gets much rounder.
  3. Change it to random 0 360 and watch the path scatter.
  4. Try dot random 5 30 inside the loop.
  5. 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
Ask GuruAcharya Samayeshwar