(Redirected from Random)

"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

The pick random () to () block is an operators block and a reporter block. The block returns a pseudorandom number ranging from the first given number to the second, including both endpoints. If both numbers are integers, the number returned will also be an integer. 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 number is a float, the number returned will be a double. For example, if 0.1 and 0.14 were given, the output could be any number between 0.1 and 0.14, with up to 17 digits after the decimal point (e.g. 0.13545101673985742).

A rainbow generator made by a pick random block.

Example Uses

Some common uses for the pick random () to () block are:

  • Creating randomized levels
when I receive [generate v]
go to x: (-50) y: (50)
set [square v] to [1]
repeat (121)
replace item (square) of [level v] with (pick random (1) to (10))
change [square v] by (1)
end
set [square v] to [1]
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]) = [10]>>
set [start v] to (pick random (1) to (121))
end
switch costume to [player v]
set [square v] to [1]
go to x: (-50) y: (50)
repeat until <(square) = (start)>
if <(x position) = [50]> 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))
  • Performing an action with a certain probabiltity
if <(pick random (1) to (10)) = (1)> then
    ...
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) ) )
Note Note: The random numbers generated from these alternatives may not be as accurate as the Pick Random () to () block.
Cookies help us deliver our services. By using our services, you agree to our use of cookies.