Coral Branch¶
By the end of this lab you'll be able to:
- Write a recursive function where each call spawns a random number of new calls
- Vary branch angles randomly with
random.uniform(-40, 40) - Shift colors from dark red at the base to pale pink at the tips using depth
A single dark red trunk rises from the sea floor and bursts into a fan of branches — some nodes sprout one branch, some two, some three — ending in a cloud of pale pink tips. This is recursion like the Fractal Tree, but with nature's randomness mixed in.
Welcome to the Coral Reef!
Real coral never branches perfectly evenly — every fork is a little
different. In this lab your recursive function will roll the dice at
every branch, just like coral growing in the ocean!
How It Works¶
The branch function draws one segment, then calls random.randint(1, 3) to decide how many sub-branches to spawn — this is called variable-arity recursion because the number of recursive calls varies. Each child turns a random amount between -40° and +40° and is a bit shorter than its parent. The depth parameter counts how many levels deep we are, and it picks both the pen size (thick at the base, thin at the tips) and the color from a dark-red-to-pink list.
Sample Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
What Do You Think Will Happen?
This program is full of random choices, but it starts with
random.seed(8). If you click Run twice in a row, will you get
the same coral both times or a different one each time?
Make your guess, then run it twice to check!
Try It Now¶
The same coral, every time — random.seed(8) restarts the random-number generator at the exact same spot, so every "random" choice repeats. Were you right?
How It Works¶
Compare this to the Fractal Tree, where every branch always splits into exactly 2 children at fixed angles. Here kids = random.randint(1, 3) makes the arity (number of recursive calls) random: a node with 1 child makes a long straggly arm, while a node with 3 children makes a dense fan. The color gradient is driven entirely by depth: at depth 0 the trunk uses colors[0] ('darkred'), and each level deeper moves one step along the list toward 'mistyrose'. Pen size does the same trick in reverse with 8 - depth, so the coral is thick where it's dark and delicate where it's pale.
Explanation Table¶
| Line | What it does |
|---|---|
kids = random.randint(1, 3) |
Each node spawns 1, 2, or 3 sub-branches — variable-arity recursion |
turn = random.uniform(-40, 40) |
Each child bends a random amount within ±40° |
monty.pencolor(colors[depth]) |
Deeper levels use lighter colors: dark red base, pink tips |
if depth >= 8 or length < 3: |
The base case — stops the recursion so it can't go on forever |
Learning Check¶
Your Turn — Grow a Fuller Coral
In the program below, random.randint(1, 3) has been changed to
random.randint(2, 3), so every node spawns at least two
sub-branches. Will this coral be fuller or scrawnier than before?
Predict, then run it!
Much fuller — guaranteeing at least 2 children at every node roughly doubles the branches at each level, so the pink canopy becomes a dense cloud.
Experiments¶
-
Roll new dice. Change
random.seed(8)to another number and run a few times. You'll know it worked when each seed grows its own unique coral. -
Calm the water. Change
random.uniform(-40, 40)torandom.uniform(-15, 15). You'll know it worked when the coral grows straighter and more tree-like instead of sprawling sideways. -
Stretch the branches. Change
length * 0.72tolength * 0.78. You'll know it worked when the coral grows taller and lankier, reaching higher up the canvas. -
Make a sun coral. Replace the color list with
['darkorange', 'orange', 'gold', 'yellow', 'khaki', 'lightyellow', 'ivory', 'white']. You'll know it worked when the coral glows orange at the base and white at the tips.
A Reef of Your Own!
Recursion plus randomness equals life-like shapes — you just proved it!
Every seed value is a different coral waiting to be discovered.
Up next: Leaf Venation — draw a leaf and give it a realistic
skeleton of veins, three levels deep.