# How to Generate 4,6,8,10 Digit Random number in PHP?

By Hardik Savani May 14, 2024 Category : PHP

This tutorial is focused on how to generate 6 digit random number in php. This article will give you simple example of generate 4 digit unique number in php. it's simple example of generate 10 digit random number in php. This post will give you simple example of php random 5 digit number.

in this post, i will give you simple example of how to generate random digit using rand() and mt_rand() function in php. let's see bellow example one by one.

rand() Example:

`<?php   /* php rand function */\$rand = rand();echo(\$rand); /* Output:  2051787537*/  /* generate 2 digit unique random number in php */\$twodigitrandom = rand(10,99);echo(\$twodigitrandom); /* Output:  63*/  /* generate 4 digit unique random number in php */\$fourdigitrandom = rand(1000,9999);echo(\$fourdigitrandom); /* Output:  5222*/  /* generate 6 digit unique random number in php */\$sixdigitrandom = rand(100000,999999);echo(\$sixdigitrandom); /* Output:  522285*/  /* generate 8 digit unique random number in php */\$eightdigitrandom = rand(10000000,99999999);echo(\$eightdigitrandom); /* Output:  95522285*/   ?>`

mt_rand() Example:

`<?php   /* php mt_rand function */\$rand = mt_rand();echo(\$rand); /* Output:  2051787537*/  /* generate 2 digit unique random number in php */\$twodigitrandom = mt_rand(10,99);echo(\$twodigitrandom); /* Output:  63*/  /* generate 4 digit unique random number in php */\$fourdigitrandom = mt_rand(1000,9999);echo(\$fourdigitrandom); /* Output:  5222*/  /* generate 6 digit unique random number in php */\$sixdigitrandom = mt_rand(100000,999999);echo(\$sixdigitrandom); /* Output:  522285*/  /* generate 8 digit unique random number in php */\$eightdigitrandom = mt_rand(10000000,99999999);echo(\$eightdigitrandom); /* Output:  95522285*/   ?>`