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. - כתוב
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).
בפסאודוקוד 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() (ללא פרמטרים) ש-מחזיר מספר שלם אקראי מ-1 עד 6 באמצעות random.randint. זכור 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), לאחר מכן השלך מטבע 20 פעמים עם random.randint(0, 1) (1 מתכוון לצד הראש), ספור את צדד הראש, והשם את הספירה בprint. עם זרע זה התשובה תמיד תהיה אותה.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.