Updated April 10, 2023

## Introduction to Lua random

Lua random is one of the mathematical concept and it’s a library function that can be used for to generate the pseudo-random numbers and it can be called in different ways like the method called without arguments and it will be return with pseudo-random real type of numbers with some uniform distributions time intervals if we call with single argument and it can be of any data types the pseudo-random numbers will return that data type values like that we called two arguments type the pseudo-random generator will used and called the randomseed functions is accept only numeric set of arguments.

**Syntax:**

Lua script have some default functions, keywords and variables for creating the applications like games, image processing etc. In that random is one of the function in the mathematical libraries and it is mainly used to generate the pseudo random numbers in the sequence order with the uniform distributions.

```
datatype variable name = math.random(Starting number, Ending number)
conditional loop {
--some validation statement based on the satisfaction of loop conditions---
}
---other lua script codes depends upon the user and project requirement----
```

The above codes are the basic syntax for calling the random function for the mathematical library in different sequences.

### How random Function Work in Lua?

- In lua script random function is one of the mathematical library and it is used to calculate the pseudo random numbers using pseudo random generator. This function is also one of the interface for to calculate the simple pseudo-random generator function it is provided by using rand calculation it is provided by some standard ANSI code language like C etc.
- It can be called in different ways like calling the function in without arguments it returns the uniform pseudo-random real numbers in the different number ranges like 0, 1 is the starting and ending position of the numbers. If the function is called the integer number as the argument it will return the integer value with the uniform pseudo-random integers in the number ranges from 1, x.
- Like that we calling the two integer numbers as the parameter arguments x and y using this math.random function will return the uniform pseudo random integers in between the ranges (m,n).
- We use math.randomseed() function also generate pseudo random generator values. The parameters are used and exactly match the local variables, functions and we used number of parameters it is different from its number of arguments and it seems to be adjusted the number of arguments and number of parameters in the multiple assignments if we use additional arguments or parameters in the method it will thrown away from the method and it gives the nil as the value when we get the extra or additional parameters.

### Examples of Lua random

Given below are the examples of Lua random:

#### Example #1

**Code:**

```
print("Your first example started:")
io.write("1 + 37832434 = ", 87325736+37326, "\n")
io.write("529837 - 3923847 = ", 590238-3387, "\n")
io.write("533 * 3347 = ", 523784*337, "\n")
io.write("528736 / 323 = ", 52353/3324, "\n")
io.write("534.2874 % 334 = ", 1235%5433, "\n")
io.write("floor(12.345) : ", math.floor(12.345), "\n")
io.write("ceil(12.34235) : ", math.ceil(12.34235), "\n")
io.write("max(2121, 123) : ", math.max(2121, 123), "\n")
io.write("min(122, 332) : ", math.min(122, 332), "\n")
io.write("pow(128, 212) : ", math.pow(128, 212), "\n")
io.write("sqrt(27) : ", math.sqrt(27), "\n")
io.write("math.random() : ", math.random(), "\n")
io.write("math.random(12) : ", math.random(10), "\n")
io.write("math.random(12,1021) : ", math.random(12,1021), "\n")
math.randomseed(os.time())
print(string.format("Pi = %.12f", math.pi))
print("Thanks for the users your first example completed successfully")
```

**Output:**

In above example we used random() function of the mathematical libraries in the script. When we are giving and passing the inputs to the method it will calculates and operates the mathematical functions it also supports and used the other mathematical functions like sqrt(), min(), max(), ceil(), floor() for each functions the user input values are calculated by using the mathematical formulas and expressions.

#### Example #2

**Code:**

```
do
vars = "Please enter your first string value it will, be calculates the string functions like length, gsub, lower and upper string values"
io.write("vars Length :\n ", string.len(vars), "\n")
io.write("Your first string value is:\n ", string.gsub(vars, "first", "value"), "\n")
io.write("Your second string value is :\n ", string.find(vars, "lower"), "\n")
io.write("Welcome To My Domain its a first example :\n ", string.upper(vars), "\n")
io.write("Please enter your first input values :\n ", string.lower(vars), "\n")
local x = 1
local function demo(x, y)
x = x + 1
if x ~= nil and y ~= nil then
return math.floor(x +(math.random(math.randomseed(os.time()+x))*1234567 %y))
else
return math.floor((math.random(math.randomseed(os.time()+x))*202))
end
end
for i = 2, 20 do
print(demo(-43, 3))
end
end
```

**Output:**

In second example we used additionally loop like do statement for checking the user input conditions first it will check and execute the conditions once the loop is executed if the condition is not satisfied the loop will terminate or exit it. Here we used some string input types as the first variable and second variable we calculate the string lengths and randomseed() function will calculate some mathematical expressions and formulas. Then it will print it on the output console. If we use os.time() function for perform the random operation by using the formulas like x * 202, x*1234567%y.

#### Example #3

**Code:**

```
local vars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz!@#$%^&*()_+-={}|[]`~*/'
local strlengths = string.len(vars)
print(strlengths)
local vars1 = ''
math.randomseed(os.time())
arrys = {}
for vars2 in vars:gmatch"." do
table.insert(arrys, vars2)
end
for i = 2, strlengths do
vars1 = vars1 .. arrys[math.random(2, #arrys)]
end
print(vars1)
```

**Output:**

In final example we declare and initialise the input values like alphabets both upper and lower case letters. Additionally, we used some operators, symbols and special characters by using string default functions we can perform the string operations and using for loop for to iterate the matched string inputs. We can pass the string inputs to the array characters and using random() method we can perform the string operations and print the output on the console.

### Conclusion

We have already seen about the lua script operations using keywords, variables and default functions. While we use the lua functions it can be performed the individual tasks based on the user needs. Like that we use mathematical operations by using the lua libraries we can create and manipulate the enterprise application datas like games, image processing etc.

### Recommended Articles

We hope that this EDUCBA information on “Lua random” was beneficial to you. You can view EDUCBA’s recommended articles for more information.