Free cookie consent management tool by TermsFeed Generator PHP random number | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP random number

PHP random number

Last Updated on Feb 15, 2023

How?

Today we are going to talk about generating random numbers in PHP. Knowing how to generate a random number is more useful than you think. you can select random users and much more.

It’s very easy to generate random numbers in PHP.

 

All you need is this function

rand()

 

rand(min value, max value);

Let's see some examples:

echo rand(1,100);
// 46
echo rand(1,100);
// 73

Random Number with Decimals

Both min value and Max value must be int so what if you wanted to generate a random number that is not int.

 

Super easy!

Generate the random number and then divide it by 100

 

// Random number between 0 and 1
echo rand(1,100) / 100;
// 0.56

// Random number between 4 and 5
echo rand(4,500) / 100;
// 4.75

Conclusion

Now you know about random numbers in PHP.

I recommend you to open a PHP files and generate some random numbers. generate numbers with decimals as well.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • generate random numbers
  • generate random numbers with decimals

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

the ultimate guide to Git: learn everything you need in less than 1 hour
Nov 28, 2022 programming

the ultimate guide to Git: learn everything you need in less than 1 hour

Here is everything you need to know about git in a series of short question and answers. ...

11 Min Read Read More
PHP Array Merge
Feb 14, 2023 programming

PHP Array Merge

Today we are going to talk about array merge function in PHP. This function is very useful and merges all the arrays that you pass as arguments and returns an array. ...

3 Min Read Read More
PHP Simple Tinker Like Script
Feb 15, 2023 programming

PHP Simple Tinker Like Script

Today we are going to build a simple Tinker in PHP. Tinker is a tool that allows users to interact with the application through the command line. ...

8 Min Read Read More
Vue.js Beginner to Advanced 2025: Complete Guide
Jul 21, 2025 programming

Vue.js Beginner to Advanced 2025: Complete Guide

Vue.js has evolved into one of the most popular JavaScript frameworks for building dynamic web applications. it continues to strike a balance between simplicity and capability, offering a progressive approach to front-end development. ...

33 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...