Randomness and simulation
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 이 페이지는 최신 브라우저(SharedArrayBuffer 지원)가 필요합니다. Chrome, Edge, Firefox 또는 Safari를 최신 버전으로 업데이트해 주세요.
English
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은 우리가 무작위 숫자를 생성할 수 있게 해줍니다.
- 우리는 또한 실제 현상을 모델링하는 작은 시뮬레이션도 만들어 보겠습니다.
English
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 가져오기(import)
- 무작위 도구들은
random이라는 모듈에 포함되어 있습니다. - 프로그램 상단에
import random을 입력합니다. - 그러면 이 모듈의 함수들을 사용할 수 있는데, 예를 들어
random.randint을 사용할 수 있습니다.
import random
number = random.randint(1, 6)
print(number)
English
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))
English
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)**는 무작위 시작 지점을 지정합니다.
- 동일한 시드를 사용하면 매번 같은 숫자를 얻습니다.
- 이는 테스트에 유용하여 결과를 반복할 수 있게 합니다.
import random
random.seed(42)
print(random.randint(1, 6))
print(random.randint(1, 6))
English
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)**은 코드를 사용하여 실제 과정을 모델링합니다.
- 여기서는 동전을 여러 번 던지고 이면(HEADS)의 개수를 세는 것을 모델링합니다.
- 많은 시도를 수행하면 장기적인 패턴을 확인할 수 있습니다.
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)
English
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)
English
Common mistakes
- A simulation uses random values to model chance.
- Run it many times to see the overall pattern.
한국어
흔한 실수
- 시뮬레이션은 확률을 모델링하기 위해 무작위 값을 사용합니다.
- 전체적인 패턴을 확인하려면 여러 번 실행해야 합니다.
English
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.
한국어
이제 직접 해보기
- 일부 과제는 특정 **성질(property)**을 확인합니다(예: "던진 값이 1과 6 사이" 등).
- 일부 과제는 출력이 매번 동일하도록 시드를 사용합니다.
- Answer 확인 버튼을 눌러 코드를 테스트하세요.
Write roll_die() (no parameters) that returns a random whole number from 1 to 6 using random.randint. Remember 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.
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.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.