Random Values · 随机值
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| unpredictable/ʌnprɪˈdɪktəbl/ | 不可预测 | bù kě yù cè |
| random/ˈrændəm/ | 随机 | suí jī |
| range/reɪndʒ/ | 范围 | fàn wéi |
| variability/ˌveərɪəˈbɪlɪti/ | 可变性 | kě biàn xìng |
| outputs/ˈaʊtpʊts/ | 输出 | shū chū |
Unpredictable on purpose
- Some programs need to be unpredictable — games, tests, simulations.
- A program can generate a random 随机 value within a specified range 范围.
RANDOM(1, 6)might return any whole number from 1 to 6, each with an equal chance.- It is like a dice built into the program.
故意的不可预测
- 有些程序需要不可预测——游戏、测试、模拟。
- 一个程序能在一个指定的范围(range)内生成一个随机(random)值。
RANDOM(1, 6)可能返回 1 到 6 的任何整数,每个机会均等。- 它像一个内置在程序里的骰子。
RANDOM(1, 6) returns: · RANDOM(1, 6) 返回:
It picks a random value within the given range. · 它在给定范围内挑一个随机值。
Variability
- Randomness introduces variability 可变性 into a program's behaviour.
- Two runs of the same program can then behave differently.
- That is exactly what makes a game feel fresh each time.
- Nothing in the code changed — only the random value did.
可变性
- 随机性把可变性(variability)引入程序的行为。
- 同一个程序的两次运行于是可以表现不同。
- 那正是让一个游戏每次感觉新鲜的东西。
- 代码里什么都没改——只有随机值变了。
Same every run, or varies? · 每次运行相同,还是变化?
A random value makes output vary between runs; a fixed calculation gives the same output every time. · 一个随机值让输出在运行之间变化;一个固定的计算每次给出相同的输出。
Randomness introduces ______ so two runs can behave differently. · 随机性引入______,所以两次运行能表现不同。
The same code can give different results each run. · 同样的代码每次运行能给出不同的结果。
roll ← RANDOM(1, 6) does what? · roll ← RANDOM(1, 6) 做什么?
The generated value is stored in the variable. · 生成的值被存进变量。
Store and use it
- You usually store a random value in a variable for later logic:
- $\text{roll} \leftarrow \text{RANDOM}(1, 6)$.
- Now
rollholds one dice value the rest of the program can use. - Random values let a program model unpredictable 不可预测 events, like a coin flip.
存储并使用它
- 你通常把一个随机值存进一个变量供以后的逻辑用:
- $\text{roll} \leftarrow \text{RANDOM}(1, 6)$。
- 现在
roll装着一个骰子值,程序其余部分能用。 - 随机值让程序为不可预测(unpredictable)的事件建模,像抛硬币。
Getting 4, then 1, then 6 from three runs of a dice program is expected, not a bug. · 一个骰子程序三次运行得 4、然后 1、然后 6 是预期的,不是缺陷。
The random value differs each run, so the output differs. · 随机值每次运行不同,所以输出不同。
A coin-flip program uses RANDOM(1, 2). The two possible outputs are: · 一个抛硬币程序用 RANDOM(1, 2)。两个可能的输出是:
1 maps to heads, 2 to tails. · 1 映射到正面,2 到反面。
Random values let a program model unpredictable events like a dice roll. · 随机值让程序为不可预测的事件建模,像掷骰子。
This makes programs feel more like the real world. · 这让程序感觉更像真实世界。
Different outputs each run
- Because of randomness, repeated runs can produce different outputs 输出.
- Running a dice program three times might give 4, then 1, then 6.
Coin flip. A program picks RANDOM(1, 2), treating 1 as "heads" and 2 as "tails". One run stores 1 → "heads"; the next stores 2 → "tails". Nothing in the code changed — only the random value differs, so the output differs. This is expected, not a bug.
每次运行不同的输出
- 由于随机性,重复运行能产生不同的输出(outputs)。
- 运行一个骰子程序三次可能给 4、然后 1、然后 6。
抛硬币。 一个程序挑 RANDOM(1, 2),把 1 当"正面",2 当"反面"。一次运行存 1 → "正面";下一次存 2 → "反面"。代码里什么都没改——只有随机值不同,所以输出不同。这是预期的,不是缺陷。
A random value is picked within a range (RANDOM(1, 6)), adding variability so two runs can differ. Store it in a variable to use it, and it lets a program model unpredictable events. Different outputs across runs are expected, not a bug.
一个随机值在一个范围内挑选(RANDOM(1, 6)),添加可变性,所以两次运行能不同。把它存进一个变量来用,它让程序为不可预测的事件建模。跨运行的不同输出是预期的,不是缺陷。