From Test-Scratch-Wiki
- "Random" redirects here. For random page on the wiki, see Special:Random.
随机取数 () 到 () | |
隨機取數 (1) 到 (10) | |
类别 | 运算类积木 |
形状 | 椭圆形积木 |
The Pick Random () to () 积木 is an 运算类积木 and a 椭圆形积木. The block picks a pseudorandom number ranging from the first given number to the second, including both endpoints. If both numbers have no decimals, it will report a whole number. For example, if a 1 and a 3 were inputed, the block could return a 1, 2 or 3. If one of the numbers has a decimal point, even .0, it reports a number with a decimal. For example, if 0.1 and 0.14 were given, the output will be 0.1, 0.11, 0.12, 0.13, or 0.14.
The numbers given with this block are not truly random — they are merely unpredictable. It is nearly impossible to generate truly random numbers using a computer.
历史
In the 实验查看器, the block had a picture of a die on it.
用法示例
In many types of 专案, random numbers must be picked — this block will easily do the job, without any complicated scripts.
Some common uses for the Pick Random () to () block:
- Creating randomized levels
當收到訊息 [generate v] 定位到 x: (-50) y: (50) 變數 [square v] 設為 [1] 重複 (121) 次 替換第 (square) 項於 [level v] 成 (隨機取數 (1) 到 (10)) 變數 [square v] 改變 (1) end 變數 [square v] 設為 [1] 重複 (11) 次 重複 (11) 次 造型換成 (清單第 (square) 項項目\( [level v] \) :: list) 蓋章 x 改變 (10) 變數 [square v] 改變 (1) end 定位到 x: (-50) y: ((y 座標) - (10)) end 重複直到 <<(清單第 (start) 項項目\( [level v] \) :: list) = [10]> 不成立> 變數 [start v] 設為 (隨機取數 (1) 到 (121)) end 造型換成 [player v] 變數 [square v] 設為 [1] 定位到 x: (-50) y: (50) 重複直到 <(square) = (start)> 如果 <(x座標) = [50]> 那麼 定位到 x: (-50) y: ((y 座標) - (10)) x 改變 (10) end 變數 [square v] 改變 (1) end 廣播訊息 [play v]
- Setting random stats
變數 [hp v] 設為 (隨機取數 (25) 到 (100))
- Choosing random objects
變數 [purchase v] 設為 (清單第 (隨機取數 (1) 到 (9)) 項項目\( [grocery list v] \) :: list)
造型換成 (隨機取數 (1) 到 (7))
应用
- Main article: List of Block Workarounds
To work around this block, the Scratcher must program a 程式 that will give unpredictable numbers.
A simple workaround is to fill a 清单 with the possible numbers, and then use the 清单第 () 项项目 block with the first input set to any. The Item (any) of () block chooses an unpredictable item — so if the chosen list has all the wanted numbers, the Item () of () block is an effective workaround:
當 @greenflag 被點擊 變數 [random number v] 設為 (清單第 (年 v) 項項目\( [list v] \) :: list)
There are scripts that can pick unpredictable numbers without using any blocks that give random values. To make these scripts, a value must be used that is unpredictable — the 计时器 is a very good choice. Other choices are:
- Continuously moving a hidden sprite around the screen and using its location
- Continuously changing a 变量 and using its current value
- Continuously changing the pen color, shade or size and using its value
Then a script can be created with the value — an example with a continuously moving sprite:
變數 [random number v] 設為 (x 座標)
This, however, is rather simple. More complicated scripts can be made:
變數 [random number v] 設為 ((x座標) + (y 座標))
變數 [random number v] 設為 (四捨五入數值 (((y 座標) + ((方向) * (2))) / (3)))
![]() | The random numbers generated from these alternatives may not be as accurate as the Pick Random () to () block. |
() + ()
• () - () • () * () • () / () • 随机取数 () 到 () • () < () • () = () • () > () • () 且 () • () 或 () • () 不成立 • 字串组合 () 和 () • 字串中第 () 字 () • 字串长度 () • ) 除 () 的馀数 • 四舍五入数值 () • () 数值 ()更多积木… |