Course 2.2Lesson 2 of 5
The random module adds chance. randint gives a whole number with both ends included, choice picks one item of a list, shuffle mixes a list itself, random gives a decimal number from 0 up to but not including 1, uniform gives a decimal number between two limits, and sample picks several different items. A seed makes the sequence repeat, which is useful when you test a program. Without a seed, each run is different. The numbers are only pseudo-random, which is enough for games and tests, but not for secrets.
Try the code and the simulations beside the lesson.
After this lesson you can
Games, simulations and quizzes need chance: a die, a shuffled deck, a random question. The module random gives it.
| Tool | What it gives |
|---|---|
random.randint(1, 6) | a whole number from 1 to 6, both included |
random.choice(items) | one item of the list |
random.shuffle(items) | mixes the list itself and gives back nothing |
random.sample(items, k) | k different items, as a new list |
random.random() | a decimal number from 0 up to, but not including, 1 |
random.uniform(a, b) | a decimal number between a and b |
random.seed(n) | makes the numbers repeat |
Predict first. Run this program twice. Which lines will change between the two runs? Then run it.
Every line changes: the die, the colour, the decimal number, the lottery numbers, the order of the cards. A few points to watch.
randint(1, 6) includes both ends, unlike range(1, 6), which stops before 6. This is a common surprise.random.sample(range(1, 50), 6) picks six different numbers, like a lottery. With randint six times, a number could repeat.random.shuffle(cards) changes the list itself. If you write cards = random.shuffle(cards), you get None, just like sort.When you test a program that uses chance, you want the same answers each time, so that you can check them. A seed does that.
The computer does not toss a coin. It follows a fixed rule that makes numbers that look random. The numbers are called pseudo-random. This is perfectly good for a game, a simulation or a test. It is not good for secrets, such as passwords. For those, there is a different module, secrets.
The seed makes the rolls in the code below repeat each time you run it, which is what you want while testing.
random.seed(1) at the top, so that the output is the same every time. When it works, remove that line.random.choice(questions) is better than a random position, because it cannot go out of range.randint(1, 6) expecting the end to be left out. Both ends are included.x = random.shuffle(x). It gives None. Just write random.shuffle(x).random.choice of an empty list. An IndexError.random.sample with more items than the list has. A ValueError.import random. A NameError.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
randint(a, b) includes both ends. choice picks one. shuffle mixes the list itself. sample picks different items.random() gives a number from 0 up to, but not including, 1.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
random.randint(1, 6) give?random.shuffle(cards) give back?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.