Night 9: Roll the dice
이미 Python 또는 의사 코드로 이날의 학습을 마쳤습니다. 두 언어 모두에서 완료한 것으로 인정됩니다. 원하면 다시 연습하세요.
이 페이지는 최신 브라우저(SharedArrayBuffer 지원)가 필요합니다. Chrome, Edge, Firefox 또는 Safari를 최신 버전으로 업데이트해 주세요.
English
Tonight: roll the dice
- Games need surprises. Tonight your pet rolls a dice.
- You will learn one thing: random numbers with
RAND.
Words for tonight
| Word | Meaning |
|---|---|
| random | you cannot know it before; it can be different each time |
| RAND | the 9618 function that gives a random number |
| INT | keep only the whole number part: INT(3.72) is 3 |
English
RAND gives a real number
RAND(6)gives a number from 0 up to 6, but never 6 itself. For example3.72.INTcuts off the part after the point.INT(3.72)is3.- So
INT(RAND(6))is 0, 1, 2, 3, 4 or 5. Add 1, and you get a dice: 1 to 6.
DECLARE Roll : INTEGER
Roll ← INT(RAND(6)) + 1
OUTPUT "Your pet rolled ", Roll
- Press Run a few times. The number changes!
English
Pick from an array
- A random index picks a random element.
DECLARE Toys : ARRAY[1:3] OF STRING
DECLARE Index : INTEGER
Toys[1] ← "ball"
Toys[2] ← "kite"
Toys[3] ← "box"
Index ← INT(RAND(3)) + 1
OUTPUT "Let's play with the ", Toys[Index]
English
How does the checker test random code?
- The checker cannot know your number. So it checks the rules:
- Is the output a number from 1 to 6? Did you use
RAND? - So use the variable names the task gives you.
English
Random plus a loop
- Put
RANDinside aWHILEloop to roll again and again.
DECLARE Roll : INTEGER
Roll ← 0
WHILE Roll <> 6
Roll ← INT(RAND(6)) + 1
OUTPUT "Rolled ", Roll
ENDWHILE
OUTPUT "Six!"
- Now roll some dice in the tasks!
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
Your pet rolls a dice. Put a random whole number from 1 부터 6 in Roll, using INT(RAND(6)) + 1. Output Roll.
출력을 보려면 '실행'을 클릭하세요.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
Pick a random index from 1 부터 3. Output Tonight: and the snack at that index.
출력을 보려면 '실행'을 클릭하세요.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
Roll two dice: DiceA 및 DiceB, each from 1 부터 6. Put their sum in Total and output it.
출력을 보려면 '실행'을 클릭하세요.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Roll a dice again and again until you get a 6. Count the rolls in Rolls. After the loop, output Six!.
출력을 보려면 '실행'을 클릭하세요.