Most simulations will start with a random (or semi-random) initial configuration. Some will introduce random noise to mimic real life uncertainty. In this tutorial, we will:

  • Use Python’s random module
  • Create multiple Particle objects

While this tutorial isn't specific to Pygame, it is import for our simulation. If you're already familiar with Python's random module then you can skip this tutorial. As before, the final code can be found by clicking the Code on Github link above.

The random module

To generate random numbers in Python (not just Pygame), we need to import the random module. This module is part of the main Python program so you don't need to download anything extra.

import random

We now have access to various functions that generate random numbers. The functions I use most often are:

  • random() - returns a random number between 0.0 and 1.0
  • uniform(a, b) - returns a random number between a and b
  • randint(a, b) - returns a random integer between a and b
  • choice(list) - returns a random element from the list
  • shuffle(list) - rearranges the order of the list

For the full range of functions see the Python documentation. For now we just need random.randint().

The code below demonstrates how to create a circle with a radius between 10 and 20 pixels (inclusive) and with a random position on the screen. To ensure that the no part of the circle is off-screen rather than picking a value between 0 and the window's dimensions, we reduce the range of values by the size of circle.

size = random.randint(10, 20)
x = random.randint(size, width - size)
y = random.randint(size, height - size)
my_random_particle = Particle((x, y), size)
my_random_particle.display()

Now, every time the program is run, unless we are very lucky, it will display a circle with a different size and position.

Creating multiple particles

Since we're using variables instead of constants for the particle's position, we can easily add multiple particles to the screen. This will be useful later because most simulations of particles are likely to have more than one. We deal with multiple Particle objects by creating an array and filling it with random Particles using a for loop. Replace the above code with the following:

number_of_particles = 10
my_particles = []

for n in range(number_of_particles):
    size = random.randint(10, 20)
    x = random.randint(size, width-size)
    y = random.randint(size, height-size)
    my_particles.append(Particle((x, y), size))

To display all the particles we need to change the line:

my_random_particle.display()

To:

for particle in my_particles:
    particle.display()

For the full program (rearranged to be slightly more logical) see the link to Github at the top of this post. If you run the program, you will see ten randomly placed and randomly sized circles, some of which may overlap. You might want to avoid having the circles overlap, but I’ll leave you to work out how to do that.

Random circles in a Pygame window

In the next tutorial, the simulation will become a bit more interesting as things get dynamic and the particles move.