(Redirected from Pick Random () to ())
- "Random" redirects here. For random page on the wiki, see Special:Random.
|Pick Random () to ()|
The Pick Random () to () block is an Operators block and a Reporter block. 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 the limits, 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 many types of projects, 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
when I receive [generate v] go to x: (-50) y: (50) set [square v] to  repeat (121) replace item (square) of [level v] with (pick random (1) to (10)) change [square v] by (1) end set [square v] to  repeat (11) repeat (11) switch costume to (item (square) of [level v]) stamp change x by (10) change [square v] by (1) end go to x: (-50) y: ((y position) - (10)) end repeat until <not <(item (start) of [level v]) = >> set [start v] to (pick random (1) to (121)) end switch costume to [player v] set [square v] to  go to x: (-50) y: (50) repeat until <(square) = (start)> if <(x position) = > then go to x: (-50) y: ((y position) - (10)) else change x by (10) end change [square v] by (1) end broadcast [play v]
- Setting random stats
set [hp v] to (pick random (25) to (100)
- Choosing random objects
set [purchase v] to (item (pick random (1) to (9)) of [grocery list v])
switch costume to (pick random (1) to (7))
- Main article: List of Block Workarounds
A simple workaround is to fill a list with the possible numbers, and then use the Item () of () 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:
when gf clicked set [random number v] to (item (any v) of [list v])
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 timer is a very good choice. Other choices are:
- Continuously moving a hidden sprite around the screen and using its location
- Continuously changing a variable 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:
set [random number v] to (x position)
This, however, is rather simple. More complicated scripts can be made:
set [random number v] to ((x position) + (y position))
set [random number v] to (round (((y position) + ((direction) * (2))) / (3)))
|Note:||The random numbers generated from these alternatives may not be as accurate as the Pick Random () to () block.|