In other languages

Help the wiki!

The Scratch Wiki is made by and for Scratchers. Do you want to contribute?

Learn more about joining as an editor!

See discussions in the Community Portal

Pick Random () to () (block)

"Random" redirects here. For random page on the wiki, see Special:Random.
Pick Random () to ()
2.0 Pick Random () to ().png
Category Operators
Type Reporter

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 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.

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.

History

Pick Random () to () in Experimental Viewer.png

In the Experimental Viewer, the block had a picture of a die on it.

Example Uses

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
  • Setting random stats
set [hp v] to (pick random (25) to (100)
switch costume to (pick random (1) to (7))

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. 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)))
Warning Note: The random numbers generated from these alternatives may not be as accurate as the Pick Random () to () block.
  • This page was last modified on 4 June 2014, at 22:38.