Randomness and simulation · Tính ngẫu nhiên và mô phỏng
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).
Những gì chúng ta sẽ làm
- Một số chương trình cần yếu tố ngẫu nhiên, như trò chơi hay thí nghiệm.
- Python có thể tạo ra các số ngẫu nhiên cho chúng ta.
- Chúng ta cũng sẽ xây dựng một mô hình mô phỏng nhỏ (một mô hình của một thực tế).
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.
Nhập module random
- Các công cụ ngẫu nhiên nằm trong một module tên là
random. - Viết
import randomở đầu chương trình của bạn. - Sau đó bạn có thể sử dụng các hàm của nó, ví dụ như
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)trả về một số nguyên từađếnb.- Cả hai đầu mút đều được bao gồm:
randint(1, 6)có thể là1,6, hoặc bất cứ số nào nằm giữa. - Chạy lại và bạn có thể sẽ nhận được một con số khác.
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.
Các số giống nhau với seed
- Một seed (thùng giống) thiết lập điểm bắt đầu ngẫu nhiên.
- Với cùng một seed, bạn sẽ nhận được cùng những con số mỗi lần.
- Điều này hữu ích cho việc thử nghiệm, giúp kết quả có thể lặp lại được.
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.
Một mô hình mô phỏng nhỏ
- Một mô hình mô phỏng mô phỏng một quy trình thực tế bằng mã nguồn.
- Ở đây chúng ta mô phỏng việc tung đồng xu nhiều lần và đếm số mặt sáuxuất hiện.
- Chạy nhiều phép thử giúp chúng ta thấy được mẫu hình dài hạn.
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).
Trong伪代码 của AP CSP
- Đề thi viết các số ngẫu nhiên với
RANDOM. RANDOM(a, b)trả về một số nguyên từađếnb, cùng ý tưởng như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.
Lỗi thường gặp
- Một mô hình mô phỏng sử dụng các giá trị ngẫu nhiên để mô phỏng sự ngẫu nhiên.
- Chạy nó nhiều lần để xem mẫu hình tổng thể.
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.
Bây giờ bạn thử
- Một số nhiệm vụ kiểm tra một tính chất (như "número quay là từ 1 đến 6").
- Một số nhiệm vụ sử dụng seed để đầu ra luôn giống nhau mỗi lần.
- Nhấn Check answer (Kiểm tra câu trả lời) để thử mã của bạn.
Write roll_die() (no parameters) that returns a random whole number from 1 to 6 using random.randint. Remember import random. · Viết roll_die() (không có tham số) mà trả về một số nguyên ngẫu nhiên từ 1 đến 6 bằng cách sử dụng random.randint. Nhớ import random.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Write roll_two() that rolls two dice (each 1 to 6) and returns their total. The total is always between 2 and 12. · Viết roll_two() lăn hai con xúc xắc (mỗi con từ 1 đến 6) và trả về tổng của chúng. Tổng luôn nằm trong khoảng từ 2 đến 12.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
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. · Sao chép chính xác: đặt random.seed(7), sau đó lật đồng xu 20 lần với random.randint(0, 1) (1 nghĩa là mặt sọc), đếm số mặt sọc, và print số lượng đó. Với seed này, câu trả lời luôn như nhau.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.