The Web Blinders logo

Programming

PHP Generating n digit random integer

We will generate a random number between digits using php's mt_rand()

<?php
function generateNDigitRandomNumber($length){
   return mt_rand(pow(10,($length-1)),pow(10,$length)-1);
}
// Example 6 digit random number
echo generateNDigitRandomNumber(6);
// Eg : 917176

$length is the Number of digits you want in the Random number

Based on $length, we will generate Minimum and Maximum $length digit positive integers, which we supply to mt_rand().

ALTERNATE TITLES

How to generate random integer php

PHP generate random number between two numbers

PHP random number generation

PHP generate random 1 digit integer / number

PHP generate random 2 digit integer / number

PHP generate random 3 digit integer / number

PHP generate random 4 digit integer / number

PHP generate random 5 digit integer / number
PHP generate random 6 digit integer / number

PHP generate random 7 digit integer / number

PHP generate random 8 digit integer / number

PHP generate random 9 digit integer / number

PHP generate random 10 digit integer / number

PHP generate random 11 digit integer / number
PHP generate random 12 digit integer / number

PHP generate random 13 digit integer / number

PHP generate random 14 digit integer / number

PHP generate random 15 digit integer / number

PHP generate random 16 digit integer / number

PHP mt_rand() example

PHP mt_rand() usage

Need developers ?

if so, send a message.

thewebblinders@gmail.com

More Programming from our blog

SEARCH FOR ARTICLES