Randomness and simulation · 随机性与模拟
What we'll do
- Some programs need chance, like games or experiments.
- Python can make random numbers for us.
- We will also build a tiny simulation (a model of something real).
我们要做什么
- 有些程序需要随机性,比如游戏或实验。
- Python 可以为我们生成随机数字。
- 我们还会做一个小小的模拟(对真实事物的一种模型)。
Import random
- The random tools live in a module named
random. - Write
import randomat the top of your program. - Then you can use its functions, like
random.randint.
导入 random
- 随机相关的工具放在一个叫
random的模块(module)里。 - 在程序最上面写
import random。 - 然后你就可以使用它的函数,比如
random.randint。
import random
number = random.randint(1, 6)
print(number)
random.randint(a, b)
random.randint(a, b)gives a whole number fromatob.- Both ends are included:
randint(1, 6)can be1,6, or anything between. - Run it again and you may get a different number.
random.randint(a, b)
random.randint(a, b)给出一个从a到b的整数。- 两端都包含在内:
randint(1, 6)可能是1、6,或中间任何一个数。 - 再运行一次,你可能得到不同的数字。
import random
for i in range(3):
print(random.randint(1, 6))
Same numbers with a seed
- A seed sets the random start point.
- With the same seed, you get the same numbers every time.
- This is useful for testing, so results can be repeated.
用种子得到相同的数字
- 种子(seed)设定随机的起点。
- 用相同的种子,你每次都会得到相同的数字。
- 这对测试很有用,因为结果可以重复。
import random
random.seed(42)
print(random.randint(1, 6))
print(random.randint(1, 6))
A tiny simulation
- A simulation models a real process with code.
- Here we model flipping a coin many times and counting heads.
- Running many trials helps us see the long-run pattern.
一个小小的模拟
- 模拟(simulation)用代码来建立真实过程的模型。
- 这里我们模拟抛硬币很多次,并数出正面的次数。
- 进行很多次试验,能帮我们看出长期的规律。
import random
random.seed(1)
heads = 0
for i in range(10):
flip = random.randint(0, 1) # 0 = tails, 1 = heads
if flip == 1:
heads = heads + 1
print(heads)
In AP CSP pseudocode
- The exam writes random numbers with
RANDOM. RANDOM(a, b)gives a whole number fromatob, the same idea asrandom.randint(a, b).
用 AP CSP 伪代码表示
- 考试用
RANDOM来生成随机数。 RANDOM(a, b)给出一个从a到b的整数,和random.randint(a, b)是同样的意思。
roll ← RANDOM(1, 6)
DISPLAY(roll)
Common mistakes
- A simulation uses random values to model chance.
- Run it many times to see the overall pattern.
常见错误
- 模拟用随机值来模仿概率。
- 多运行几次才能看到整体规律。
Now you try
- Some tasks check a property (like "the roll is between 1 and 6").
- Some tasks use a seed so the output is the same every time.
- Press Check answer to test your code.
现在轮到你
- 有些任务检查一个性质(比如"这个点数在 1 到 6 之间")。
- 有些任务使用种子,所以输出每次都一样。
- 按检查答案来测试你的代码。
Write roll_die() (no parameters) that returns · 返回值 a random whole number from 1 to 6 using random.randint. Remember import random. · 写一个 roll_die()(没有参数),用 random.randint 返回一个从 1 到 6 的随机整数。记得 import random。
Click Run to see the output here. · 点击“运行”查看此处输出。
Write roll_two() that rolls two dice (each 1 to 6) and returns · 返回值 their total. The total is always between 2 and 12. · 写一个 roll_two(),掷两个骰子(每个 1 到 6),返回它们的总和。总和总是在 2 到 12 之间。
Click Run to see the output here. · 点击“运行”查看此处输出。
Copy this exactly: set random.seed(7), then flip a coin 20 times with random.randint(0, 1) (1 means heads), count the heads, and print the count. With this seed the answer is always the same. · 完全照这样做:设置 random.seed(7),然后用 random.randint(0, 1) 抛硬币 20 次(1 表示正面),数出正面的次数,并 print 这个次数。用这个种子,答案每次都一样。
Click Run to see the output here. · 点击“运行”查看此处输出。