Random Circles¶
By the end of this lesson you'll be able to:
- Import the
randomlibrary and generate random integers withrandint() - Use random numbers to place a turtle at an unpredictable position
- Draw a canvas full of colorful circles that look different every time you run the program
So far every program you've written produces the exact same drawing every time. The random library changes that — it generates numbers that are unpredictable, so each run of your program is a surprise.
Welcome to This Lesson!
What if you could make a program that draws something different every single time you run it?
That's the magic of randomness — and today I'm going to show you how!
Let's code it together!
Generating Random Numbers¶
To use random numbers, you first need to import the random library — just like you import turtle. Then use randint(min, max) to get a random whole number between min and max (both inclusive):
1 2 | |
Each time this line runs, x gets a different value somewhere between -150 and 150.
Scratch Bridge
In Scratch you used the "Pick random 1 to 10" block.
In Python, random.randint(1, 10) does exactly the same thing — picks a random whole number
in that range each time it runs!
Sample Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
What Do You Think Will Happen?
The loop runs 10 times, picking a new random position and color each time.
How many circles will appear? Will they always be in the same spots?
Make your guess — then click Run twice to see what changes!
Try It Now¶
Edit the code below and click Run to see the result right on this page. No account needed — everything runs in your browser.
Were you right? Ten colorful circles scatter across the canvas in random positions — and clicking Run again produces a completely different arrangement!
How It Works¶
random.randint(-150, 150) returns a different integer each time — Python uses a pseudorandom algorithm that produces unpredictable-looking numbers. goto(x, y) jumps the turtle to that position, and circle(10) draws a circle with a radius of 10 steps around the current position.
speed(10) sets the turtle to maximum drawing speed so the circles appear quickly instead of crawling into place.
Explanation Table¶
| Line | What it does |
|---|---|
import random |
Loads the random number library |
random.randint(-150, 150) |
Returns a random integer between -150 and 150 (inclusive) |
monty.speed(10) |
Sets drawing speed to maximum (10 = fastest) |
colors[random.randint(0, 5)] |
Picks a random index from 0 to 5 to select a color from the list |
monty.circle(10) |
Draws a circle with radius 10 at the current position |
Index Out of Range
If your colors list has 6 items (indexes 0–5), then randint(0, 6) can produce index 6 — which doesn't exist!
Always use randint(0, len(colors) - 1) or randint(0, 5) when the list has 6 items.
Learning Check¶
Your Turn — Make the Circles Bigger
The program below draws 10 random circles but they're all the same tiny size.
Add one line inside the loop to pick a random radius between 5 and 25,
store it in a variable called radius, and use it in monty.circle(radius).
Experiments¶
Try these changes. Predict first, then run and compare!
-
Change
range(10)torange(30). You'll know it worked when 30 circles fill the canvas instead of 10. -
Change
randint(-150, 150)torandint(-50, 50)for bothxandy. You'll know it worked when all the circles cluster near the center of the canvas. -
Add
'gold','cyan', and'brown'to thecolorslist, and updaterandint(0, 5)to match the new list length. You'll know it worked when you occasionally see gold, cyan, or brown circles. -
Change
monty.circle(10)tomonty.circle(random.randint(5, 25)). You'll know it worked when the circles vary in size, some tiny and some large. -
Change the number of circles drawn by storing the count in a variable
num_circles = 20at the top and using it inrange(num_circles). You'll know it worked when changingnum_circlesin one place controls how many appear.
Great Work!
Every run is a unique piece of art — that's the power of randomness!
In the next lab you'll learn more about lists and how to loop through them directly.
Let's code it together!