Summary
Generates a random value.
Syntax
local someRealNum = random()local someInteger = random(maxNumber)
local someNumberInRange = random(minNumber, maxNumber)
local someValue = random(table)
Parameters
maxNumber : Integer number indicating the maximum value in the range. minNumber : Integer number indicating the minimum value in the range. table : Table containing the possible values.
Returns
Random value, depending on the parameters. See below for details.
Description
This function generates a random number by using Windows cryptographically secure pseudorandom number generator, and returns a value depending on the calling parameters.
- If the function is called without parameters, it returns a random real number between 0.0 and 1.0
- When called with a number parameter, it returns a random integer between 1 and the specified value
- When called passing two numbers as parameters, it returns a random integer between the first and the second values
- When called passing a table as a parameter, it returns an element from the table choosen at random
Examples
local randomPercentage = random() * 100
local randomAge = random(99)
local randomIpAddress = '192.168.1.' .. random(1, 255)
local randomDay = random({'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'})
print('Percentage: ' .. randomPercentage .. '%')
print('Age: ' .. randomAge)
print('ip address: ' .. randomIpAddress)
print('Day of the week: ' .. randomDay)
Percentage: 87.224373774242%
Age: 45
ip address: 192.168.1.180
Day of the week: Wednesday