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 อยู่ใน โมดูล ชื่อ
random - เขียน
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
- Seed กำหนดจุดเริ่มต้นของการสุ่ม
- ด้วย 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.
การจำลองขนาดเล็ก
- การจำลอง สร้างแบบจำลองกระบวนการจริงด้วยโค้ด
- ที่นี่เราจำลองการโยนเหรียญหลายครั้งและนับด้านหัว
- การรันหลายรอบช่วยให้เห็นรูปแบบในระยะยาว
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).
In AP CSP pseudocode
- ข้อสอบเขียนตัวเลขสุ่มด้วย
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")
- บางโจทย์ใช้ seed เพื่อให้ผลลัพธ์เหมือนกันทุกครั้ง
- กด Check answer เพื่อทดสอบโค้ดของคุณ
Write roll_die() (no parameters) that returns a random whole number from 1 to 6 using random.randint. Remember import random. · เขียน roll_die() (ไม่มีพารามิเตอร์) ที่ คืนค่า จำนวนเต็มสุ่มจาก 1 ถึง 6 โดยใช้ random.randint จำ import random ได้
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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), แล้วพลิกเหรียญ 20 ครั้งด้วย random.randint(0, 1) (1 คือหัว), นับหัว, และ print จำนวนนับ With seed นี้คำตอบจะเหมือนกันเสมอ
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่