Random Numbers

Every time you run your scene, everything appears in exactly the same place. That’s useful for control — but real worlds feel alive because things are unpredictable.

Python’s random module lets you generate numbers that are different every time your code runs. Use them to scatter objects across a scene, pick colors on the fly, or animate things that move in surprising ways.

Open In Jupyter K-12

random.randint() — A Random Whole Number

import random
height = random.randint(1, 8)

random.randint(a, b) returns a random integer (whole number) between a and b, including both endpoints. Think of it like rolling a custom die — randint(1, 6) simulates a standard six-sided die.

Run the cell below a few times. The sphere lands at a different height each time!

import scene3d
import random

scene = scene3d.Scene()
scene.set_sky('#1a1a2e')
scene.set_ground(length=10, width=10)

height = random.randint(1, 8)

sphere = scene3d.Shapes.Sphere(diameter=1, segments=16)
sphere.set_color('#e94560')
sphere.set_position(0, height, 0)
scene.add(sphere)

ctx = scene.get_context('2d')
ctx.fill_style = '#ffffff'
ctx.font = '22px sans-serif'
ctx.fill_text(f'Height: {height}', 10, 30)

{ “question_type”: “multiple_choice”, “question”: “What real-world thing does random.randint(1, 6) simulate?”, “options”: [ { “key”: “a”, “text”: “Flipping a coin” }, { “key”: “b”, “text”: “Rolling a six-sided die” }, { “key”: “c”, “text”: “Picking a card from a deck” }, { “key”: “d”, “text”: “Spinning a wheel with 10 sections” } ], “answer”: “b”, “submitted_answer”: “” }

random.uniform() — A Random Decimal Number

x = random.uniform(-8, 8)

random.uniform(a, b) returns a random float (decimal number) anywhere between a and b. This is perfect for placing objects at precise positions in 3D space, where you want full coverage of a range — not just whole numbers.

Run the cell below. Each time, 25 spheres appear at completely different positions!

import scene3d
import random

scene = scene3d.Scene()
scene.set_sky('#0f3460')
scene.set_ground(length=20, width=20)

for i in range(25):
  sphere = scene3d.Shapes.Sphere(diameter=0.5, segments=10)
  x = random.uniform(-8, 8)
  z = random.uniform(-8, 8)
  sphere.set_position(x, 0.3, z)
  sphere.set_color('#f5a623')
  scene.add(sphere)

{ “question_type”: “multiple_choice”, “question”: “What is the difference between random.randint(1, 10) and random.uniform(1, 10)?”, “options”: [ { “key”: “a”, “text”: “randint is faster; uniform is slower” }, { “key”: “b”, “text”: “randint returns a whole number; uniform returns a decimal” }, { “key”: “c”, “text”: “randint includes 10; uniform does not” }, { “key”: “d”, “text”: “They return exactly the same values” } ], “answer”: “b”, “submitted_answer”: “” }

random.choice() — Pick from a List

colors = ['#e94560', '#f5a623', '#4488ff']
color = random.choice(colors)

random.choice(list) picks one item at random from a list. It’s perfect for choosing a random color, a random shape, or a random word from a collection you’ve defined.

The scene below combines random.choice() with random.uniform() to scatter colorful spheres across the ground.

import scene3d
import random

scene = scene3d.Scene()
scene.set_sky('#0f3460')
scene.set_ground(length=20, width=20)

colors = ['#e94560', '#f5a623', '#4488ff', '#44cc88', '#cc44ff', '#ff8844']

for i in range(20):
  sphere = scene3d.Shapes.Sphere(diameter=0.7, segments=12)
  x = random.uniform(-8, 8)
  z = random.uniform(-8, 8)
  sphere.set_color(random.choice(colors))
  sphere.set_position(x, 0.45, z)
  scene.add(sphere)

{ “question_type”: “multiple_choice”, “question”: “What does random.choice([‘red’, ‘blue’, ‘green’]) return?”, “options”: [ { “key”: “a”, “text”: “All three items at once” }, { “key”: “b”, “text”: “A random number between 0 and 2” }, { “key”: “c”, “text”: “One randomly selected item from the list” }, { “key”: “d”, “text”: “Always the first item in the list” } ], “answer”: “c”, “submitted_answer”: “” }

Randomness in Animation

Random numbers aren’t just for placing objects at the start — you can use them during animation too. The scene below creates a rain effect. Each drop falls at a random speed, and when it hits the ground it reappears at a new random position at the top.

Click Stop (■) when you’ve watched the rain for a while.

import scene3d
import random

scene = scene3d.Scene()
scene.set_sky('#0a0a1e')
scene.set_ground(length=20, width=20)

drops = []
for i in range(15):
  drop = scene3d.Shapes.Cylinder(diameter=0.1, height=0.5, tessellation=6)
  drop.set_color('#88aaff')
  x = random.uniform(-8, 8)
  y = random.uniform(3, 12)
  z = random.uniform(-8, 8)
  speed = random.uniform(3, 8)
  drop.set_position(x, y, z)
  scene.add(drop)
  drops.append({'mesh': drop, 'x': x, 'y': y, 'z': z, 'speed': speed})

@scene.on_frame
def animate(dt):
  for drop in drops:
    drop['y'] -= drop['speed'] * dt
    if drop['y'] < 0:
      drop['y'] = random.uniform(8, 14)
      drop['x'] = random.uniform(-8, 8)
      drop['z'] = random.uniform(-8, 8)
      drop['speed'] = random.uniform(3, 8)
    drop['mesh'].set_position(drop['x'], drop['y'], drop['z'])

scene.run()

{ “question_type”: “true_false”, “question”: “If you call random.seed(42) before generating numbers, you will always get the same sequence of random values.”, “answer”: “True”, “submitted_answer”: “” }

{ “question_type”: “freeform”, “question”: “Write the function call that picks a random integer between 1 and 100, including both 1 and 100.”, “answer”: “random.randint(1, 100)”, “submitted_answer”: “” }

Try It Yourself

Use the sliders to control how many spheres appear and which seed is used.

A seed is a starting value for the random number generator. The same seed always produces the same sequence of numbers — so you can “freeze” a layout you like by keeping the seed fixed, and change the seed to explore a totally different arrangement.

import scene3d
import random

COUNT = 20 #@param {type:"slider", min:5, max:50, step:5}
SEED  = 42 #@param {type:"slider", min:1, max:100, step:1}

random.seed(SEED)

scene = scene3d.Scene()
scene.set_sky('#0f3460')
scene.set_ground(length=20, width=20)

colors = ['#e94560', '#f5a623', '#4488ff', '#44cc88', '#cc44ff', '#ff8844', '#44ffcc']

for i in range(COUNT):
  sphere = scene3d.Shapes.Sphere(diameter=0.6, segments=10)
  x = random.uniform(-8, 8)
  z = random.uniform(-8, 8)
  sphere.set_color(random.choice(colors))
  sphere.set_position(x, 0.4, z)
  scene.add(sphere)

Think about a game or simulation you’ve played where randomness made things interesting — a shuffled deck, a procedurally generated map, enemies that move unpredictably.

Describe one way you’d use random numbers in a 3D scene of your own. Which function would you use — randint, uniform, or choice — and why?