- "Random" redirects here. For a random page on the wiki, see Special:Random.
| Pick Random () to () | |
pick random () to () | |
| Category | Operators |
| Type | Reporter |
| Introduced in | 11Oct03 (0.x) |

The pick random () to () block is an operators block and a reporter block. The block returns a pseudorandom number between the range of the two input numbers with approximately uniform probability.
Behavior
If both numbers are integers, it will return an integer inclusively between the two inputs. For example, if a 1 and a 3 were the bounds, the block might return either a 1, 2, or 3.
If one or both of the inputs are not integers, the number returned will be an unrounded double-precision floating-point number, exclusive of the upper bound[1]. For example, if 0.1 and 0.14 were given, the output will always be ≥ 0.1 and < 0.14 with up to 17 digits after the decimal point, such as 0.11804481813670736.
Example Uses
Some common uses for the pick random () to () block are:
- Setting random stats
set [hp v] to (pick random (25) to (100))
- Choosing random objects
set [purchase v] to (item (pick random (1) to (length of [grocery list v])) of [grocery list v])
switch costume to (pick random (1) to (7))
- Performing an action with a certain probability
if <(pick random (1) to (10)) = (1)> then // 10% chance of being true
...
end
or
if <(pick random (0.0) to (1.0)) < (0.1)> then // 10% chance of being true
...
end
- Creating randomized levels
define stamp ground tiles set y to (-160) repeat (9) set x to (-220) repeat (12) switch costume to (join [grass] (pick random (1) to (5))) stamp change x by (40) end change y by (40) end
Workaround
- Main article: List of Block Workarounds
To work around this block, the Scratcher must program a script that will give unpredictable numbers.
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 or random. The Item (random) 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 green flag clicked set [random number v] to (item (join [random] () ) of [list v] )
There are scripts that can return unpredictable numbers without using any blocks that give random values. To make these scripts, a value must be used that is unpredictable, such as the timer. 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) ) )
| The random numbers generated from these alternatives may not be as accurate as the Pick Random () to () block. |
Seed
Pseudorandom number generators are algorithms that transform an inputted seed value into a number that is seemingly random. Running the algorithms again with the same seed will produce the same random number.
The seed for the pick random block is hidden and can not be set by the user. It also changes every time the block runs so successive runs of the block will produce different random numbers. Control over the seed is sometimes desired, such as for replaying procedurally-generated levels and terrains.[2]
A block with a seed input has been suggested.[3]
There are many algorithms that may be implemented as a workaround. One of the simpler kinds are linear congruential generators that involve repeated multiplication, addition and modulo operations.
Simple example:
set [result v] to (12) // input the seed repeat (10) // number of iterations set [result v] to ((((result) * (7457)) + (33461)) mod (65536)) end
References
- ↑ blocks/scratch3_operators.js implements Math.random()
- ↑ topic:103062
- ↑ topic:560295
|
() + () • () - () • () * () • () / () • Pick Random () to () • () < () • () = () • () > () • () and () • () or () • Not () • Join ()() • Letter () of () • Length of () • () Mod () • Round () • () of ()More blocks...
|