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.
نفس الأرقام باستخدام البذرة
- البذرة تحدد نقطة البدء العشوائية.
- باستخدام نفس البذرة، تحصل على نفس الأرقام دائمًا.
- هذا مفيد للاختبار، بحيث يمكن تكرار النتائج.
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. · اضغط تشغيل لرؤية المخرجات هنا.