Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
407 views
in Technique[技术] by (71.8m points)

php - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...