🎲 Random Numbers

5th Grade 💻 Coding

A random number is one that’s picked by chance, so you can’t guess ahead of time what it will be. Games use random numbers so they aren’t predictable.

Free · US Letter · no sign-up
Google Classroom Save to Pinterest Email me

In Python, you write import random and then random.randint(1, 6), which gives a whole number from 1 to 6, including both, just like rolling a die. Scratch has a green block, “pick random 1 to 10.” With them, a coin can appear in a different place each time, and cards get shuffled.

Here’s an interesting fact: a regular computer doesn’t really “draw” random numbers. It uses a clever formula that makes a series of numbers that look random. This is called pseudo-randomness, and for games it’s plenty good. For sensitive things like encryption, special methods are used that are much harder to guess.

For example

  • Rock, paper, scissors against the computer: it picks a random number from 1 to 3 and chooses based on it.
  • In a guessing game, secret = random.randint(1, 100) picks the secret number.
  • In Scratch, you can send a sprite to a random spot: “go to x: (pick random -200 to 200)”.

📌 Remember: randint(1, 6) = a die in code.

Quick quiz

  1. What does random.randint(1, 6) do?

  2. Why do games use random numbers?

  3. What is pseudo-randomness?

🔎 Ask Thinkapedia another question