• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
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.  Get First Element of Array in php
  • 3.  php array_splice() – Remove, Insert, and Replace Elements in Array
  • 4.  php pi – Get Value of pi Using php pi() Function
  • 5.  php Print Array – How to Print All Elements in Array
  • 6.  php array_pop() – Remove Last Element from Array
  • 7.  php array_map() – Create New Array from Callback Function
  • 8.  php acosh – Find Hyperbolic Arccosine of Number Using acosh() Function
  • 9.  php array_combine() – Create New Array Given Arrays of Keys and Values
  • 10.  php preg_match_all – Get All Matches of Pattern in String

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 *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

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