Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate an N-digit random number

I want to generate a 6 digit random number using the PHP mt_rand() function.

I know the PHP mt_rand() function only takes 2 parameters: a minimum and a maximum value.

How can I do that?

like image 968
Saleh Avatar asked Feb 14 '11 09:02

Saleh


People also ask

How do you generate a 7 digit random number in Python?

randint() to generate a random number of length N. Call random. randint(a, b) to return a random number between a and b , inclusive.

What is an n digit number?

An n digit number is a positive number with exactly n digits. Nine hundred distinct n digit numbers are to be formed using only the three digits 2, 5, and 7. The smallest value of n for which this is possible is. (a) 6. (b) 7.

How do you generate a random 5 digit number in Java?

Just generate a int value = random. nextInt(100000) so that you will obtain a value in [0,99999] . Now your definition of 5 digits pin is not precise, 40 could be interpreted as 00040 so it's still 5 digits if you pad it.


1 Answers

Something like this ?

<?php  $a = mt_rand(100000,999999);  ?> 

Or this, then the first digit can be 0 in first example can it only be 1 to 9

for ($i = 0; $i<6; $i++)  {     $a .= mt_rand(0,9); } 
like image 80
Marco Avatar answered Oct 02 '22 18:10

Marco