To generate a random integer using php, you can use the php **random_int()** function.

`echo random_int(0,100); // Output: 54`

Random number generation with php is easy to do.

If you want to generate random integers, you can use **rand()**, but if you want to generate cryptographically secure integers, then you can use the php **random_int()** method.

The php **random_int()** method generates cryptographically secure random integers.

To use **random_int()**, pass two integers. Then **random_int()** will return a random integer between those integers.

Below are some examples showing you how to use **random_int()** in PHP.

`echo random_int(0,100); // Output: 63`

## Generating Random Integers with random_int() in a Range in php

Generating random integers in a range of numbers using php is very easy. We can use the **rand()** or **mt_rand()** and pass the two numbers of our range to the function.

If we want to generate an integer with **random_int()** between 0 and 100, we can do so with the following php code:

`echo random_int(0,100); // Output: 72`

## Using random_int() to Generate Random Integers in a Loop

Being able to generate one random integer is great, but what if we want to generate many random integer?

We can generate multiple random integers with php by using the **rand()** function and putting it in a loop.

Let’s generate 10 random integers between 0 and 1 and stick them in an array.

```
$random_array = array();
for ($x = 0; $x < 10; $x++) {
array_push($random_array,random_int(0, 100));
}
print_r($random_array);
// Output:
Array
(
[0] => 6
[1] => 25
[2] => 73
[3] => 80
[4] => 24
[5] => 3
[6] => 97
[7] => 44
[8] => 100
[9] => 31
)
```

Hopefully this article has been useful for you to understand how to generate random integers with **random_int()** using php.

## Leave a Reply