math/random

From Garry's Mod
Jump to: navigation, search
math.random( )

Contents

Description

If no value is specified, a random float value between 0-1 will be returned, if the first value is specified, a integer value between 1 and the specified value will be returned, if both values are specified, a integer value between the first and the second value will be returned, all inputs are rounded. If the first value is larger as the second value an error like: bad argument #2 to 'random' (interval is empty) will be thrown.

Arguments

number limitOrLimitStart

Limit or begin of limit.

Arguments

number limit

End of limit.

Returns

number

maximum value

Examples

Example

Generate a random number between 1 and 20.

print(math.random(1,20))

Output: 4


Examples

Example

Generate a random number between 1 and 400 with both math.random and math.Rand.

print(math.random(1,400))

print(math.Rand(1,400))

Output: 317

1.0162317198768


Examples

Example

Generate a random number between 1 and 10, if the result is below or equal to 3 execute code, else if the number is above 3 execute different code.

if math.random(1,10) <= 3 then
    print("A random value between 1 and 10 was equal to or below 3.")
else
    print("A random value between 1 and 10 was above 3.")
end

if math.Rand(1,10) <= 3 then
    print("A random value up to 13 decimal points between 1 and 10 was equal to or below 3.")
else
    print("A random value up to 13 decimal points between 1 and 10 was above 3.")
end

Output: 2

print("A random value between 1 and 10 was equal to or below 3.")

7

print("A random value between 1 and 10 was above 3.")

2.3333333333333

print("A random value up to 13 decimal points between 1 and 10 was equal to or below 3.")

7.7777777777777

print("A random value up to 13 decimal points between 1 and 10 was above 3.")

Personal tools
Navigation