Random

Add a touch of random to your patterns.

The Random Command

random n
Generates a random number between 0 and n-1.

Using in Pattern Code

This code picks a random colour and points to a random angle. Can you see how the random number is chosen to be in suitable range for either the colour or the angle?

repeat 500 [
  home
  setpc random 15
  rt random 360
  fd 18
]

Combining with Shape Patterns

This code draws 150 random squares on the screen. Can you change it to make triangles, or hexagons?

rpt 150 [
    make "length random 5
    make "colour random 15
    if (:colour <> 7) [ ; no white squares
      setps 5 + random 21 ; pen size between 5 and 20
      setpc :colour
      setxy random 1489 random 855
      rpt 4 [ fd 1+:length rt 90 ]
  ]
]