• 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 Number Generator with rand Function

php Random Number Generator with rand Function

December 15, 2021 Leave a Comment

To generate a random number using php, the simplest way is to use the php rand() function:

echo rand(); // Output: 1123 
echo rand(0,10); // Output: 3

Random number generation with php is easy to do. All we need is the php rand() method.

The php rand() method generates random integers. There are two ways you can call rand() to generate a random integer.

First, if you want to generate a random integer between 0 and 2147483647 (this is platform dependent though), you can call rand() without passing the min and max parameters:

echo rand(); // Output: 4178 

If you want to generate a random number in a range of numbers using php, you can call rand() and pass the minimum and maximum of your range:

echo rand(10,20); // Output: 16 

You can also use the mt_rand() function which uses the Mersenne Twister algorithm to generate random numbers.

echo mt_rand(); // Output: 84761 

Generating Random Numbers 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 between 0 and 100, we can do so with the following php code:

echo rand(0,100); // Output: 34 

Generating Random Floats in a Range with php

The rand() and mt_rand() functions only generate integers, so if we want to generate random float numbers in php, we need to create a function.

To generate a random float in php, we can use the function below:

function randomFloat($min = 0, $max = 1) {
    return $min + mt_rand()  * ($max - $min) / mt_getrandmax();
}

Let’s generate a few random floats in the range 20 to 30 – the following php code will do this for us:

function randomFloat($min = 0, $max = 1) {
    return $min + rand()  * ($max - $min) / getrandmax();
}

echo randomFloat(20,30); // Output: 28.981979893978
echo randomFloat(20,30); // Output: 20.341044915999
echo randomFloat(20,30); // Output: 21.3420155185

Using php to Generate Random Numbers in a Loop

Being able to generate one random number is great, but what if we want to generate many random numbers? We can generate multiple random numbers with php by using the rand() function and putting it in a loop.

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

function randomFloat($min = 0, $max = 1) {
    return $min + rand()  * ($max - $min) / getrandmax();
}

$random_array = array();

for ($x = 0; $x < 10; $x++) {
  array_push($random_array,randomFloat());
}

print_r($random_array);

// Output:
Array
(
    [0] => 0.39922291990333
    [1] => 0.71742709154143
    [2] => 0.97393983089083
    [3] => 0.046793667621349
    [4] => 0.076585880050708
    [5] => 0.59696800382667
    [6] => 0.66097702209883
    [7] => 0.77552434372507
    [8] => 0.54100747711072
    [9] => 0.12491378706177
)

Hopefully this article has been useful for you to understand how to generate a random number in a range of numbers using php.

Other Articles You'll Also Like:

  • 1.  php array_flip() – Flip the Keys and Values of an Array in php
  • 2.  Using function_exists() to Check if Function Exists in php
  • 3.  Get Days in Month Using php
  • 4.  php is_array() Function – Check if Variable is Array in php
  • 5.  php str_replace – Replace String in php
  • 6.  How to Get Session ID in php with session_id()
  • 7.  How to Add Items to Array in php
  • 8.  Get Array Length with php count() Function
  • 9.  php in_array – Check If Value is in php Array
  • 10.  Capitalize First Letter of String with php ucfirst() Function

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