5. Randomness

An evolution sim needs randomness — creatures start in random spots, move in random directions, and their babies are slightly random too. That randomness is what makes evolution interesting. This page shows how to add luck to your programs.


Turning on the random tools

Python keeps its random tools in a toolbox called random. You bring it in with import at the very top of your file.

import random

You only need to write import random once, at the top. After that you can use all its tools.


Pick a random item from a list — random.choice()

random.choice() reaches into a list and pulls out one item at random.

import random

directions = ["north", "south", "east", "west"]
chosen_direction = random.choice(directions)

print(f"The creature wanders {chosen_direction}.")

Output (yours may differ — that’s the point!):

The creature wanders east.

Run it again and you’ll probably get a different direction. That’s randomness at work.

This is perfect for choosing a random creature to have a baby, or a random spot to place some food.


A random whole number — random.randint()

random.randint(low, high) gives you a random whole number between low and high. Both ends are included.

import random

energy_from_food = random.randint(5, 15)
print(f"This berry gave {energy_from_food} energy.")

Output (varies):

This berry gave 11 energy.

random.randint(1, 6) is like rolling a dice — you get 1, 2, 3, 4, 5, or 6.


A random decimal — random.random()

random.random() gives a decimal number from 0.0 up to (but not quite reaching) 1.0. This is brilliant for “does this happen or not?” chances.

import random

mutation_chance = random.random()   # somewhere between 0.0 and 1.0
print(mutation_chance)

Output (varies):

0.3719283...

Using it for a percentage chance

Say you want a mutation to happen 10% of the time. 10% is 0.1. So: if the random number is less than 0.1, the mutation happens.

import random

if random.random() < 0.1:
    print("A mutation happened! This baby is a bit different.")
else:
    print("No mutation this time — the baby is like its parent.")

Because random.random() is a fresh random number every time, roughly 1 time in 10 you’ll see the mutation message. Change 0.1 to 0.5 and it’ll happen about half the time.


Why randomness makes evolution work

Here’s the big idea behind your sim:

  1. Creatures are all slightly different (some faster, some slower) — thanks to random mutations.
  2. The differences that help a creature survive and have babies get passed on.
  3. Over many generations, the whole group slowly changes to suit the world.

Randomness is the spark. Without it, every creature would be identical and nothing would ever improve. You’ll use random.choice, random.randint and random.random all over your sim.


A tiny example: a slightly-mutated baby

Here’s how you might make a baby creature that’s almost like its parent, but with a small random change to its speed:

import random

parent_speed = 5

# the baby's speed is the parent's, nudged up or down a little at random
speed_change = random.randint(-1, 1)     # -1, 0, or +1
baby_speed = parent_speed + speed_change

print(f"Parent speed: {parent_speed}")
print(f"Baby speed:   {baby_speed}")

Output (varies):

Parent speed: 5
Baby speed:   6

Do this for lots of babies over lots of generations, keep the faster ones alive, and — evolution!


✅ Quick recap

  • Write import random once at the top of your file.
  • random.choice(a_list) picks a random item from a list.
  • random.randint(1, 6) gives a random whole number (both ends included).
  • random.random() gives a decimal from 0.0 to 1.0 — great for chances.
  • if random.random() < 0.1: makes something happen about 10% of the time.