• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / PHP / PHP random_int() Function – Generate Random Integers in PHP

PHP random_int() Function – Generate Random Integers in PHP

August 8, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  php unset – How to Destroy Variables in php
  • 2.  How to Get Current System IP Address in php
  • 3.  Get Word Count of String in php with str_word_count() Function
  • 4.  Remove Specific Character from String Variable in php
  • 5.  Using strtoupper() in php to Convert String to Uppercase
  • 6.  php strlen() – Get the Length of String Variable in php
  • 7.  preg_replace php – Use Regex to Search and Replace
  • 8.  How to Get Session ID in php with session_id()
  • 9.  php preg_match_all – Get All Matches of Pattern in String
  • 10.  php is_float() Function – Check if Variable is Float in php

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy