Lingo Dictionary > O-R > random() |  |

random()

Syntax

    random(integerExpression)

Description

Function; returns a random integer in the range 1 to the value specified by `integerExpression`.`` This function can be used to vary values in a movie, such as to vary the path through a game, assign random numbers, or change the color or position of sprites.

To start a set of possible random numbers with a number other than 1, subtract the appropriate amount from the `random()` function. For example, the expression `random(n + 1) - 1` uses a range from 0 to the number `n`.

Example

This statement assigns random values to the variable `diceRoll`:

    set diceRoll = random(6) + random(6)

This statement randomly changes the foreground color of sprite 10:

    sprite(10).forecolor = random(256) - 1

Example

This handler randomly chooses which of two movie segments to play:

    on SelectScene
    	if random(2) = 2 then
    		play frame "11a"
    	else
    		play frame "11-b"
    	end if
    end

Example

The following statements produce results in a specific range.

This statement produces a random multiple of 5 in the range 5 to 100:

    theScore = 5 * random(20)

This statement produces a random multiple of 5 in the range 0 to 100:

    theScore = 5 * (random(21) - 1)

This statement generates integers between -10 and +10:

    dirH = random(21) - 11

This statement produces a random two-point decimal value:

    the floatPrecision = 2
    theCents = random(100)/100.0 - .01