Three ways to generate random numbers in PHP

Source: Internet
Author: User
Tags shuffle

Three ways to generate random numbers in PHP

How do I generate a random number between 1-10 in PHP?

Example 1, use the shuffle function to generate a random number.


<?php
$arr =range (1,10);
Shuffle ($arr);
foreach ($arr as $values)
{
echo $values. " ";
}
?>

Example 2, use the Array_unique function to generate a random number.


<?php
$arr =array ();
while (count ($arr) <10)
{
$arr []=rand (1,10);
$arr =array_unique ($arr);
}
echo Implode ("", $arr);
?>

Example 3, using the Array_flip function to generate random numbers, you can remove duplicate values.


<?php
$arr =array ();
$count 1=0;
$count = 0;
$return = Array ();
while ($count < 10)
{
$return [] = Mt_rand (1, 10);
$return = Array_flip (Array_flip ($return));
$count = count ($return);
}//www.jb51.net
foreach ($return as $value)
{
echo $value. " ";
}
echo "<br/>";
$arr =array_values ($return);//Get the value of the array
foreach ($arr as $key)
echo $key. " ";
?>

PHP Random number Generation Function Example
?

<?php
function RANDPW ($len =8, $format = ' all ') {
$is _ABC = $is _numer = 0;
$password = $tmp = ";
Switch ($format) {
Case ' all ':
$chars = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ';
Break
Case ' CHAR ':
$chars = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz ';
Break
Case ' number ':
$chars = ' 0123456789 ';
Break
Default:
$chars = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ';
Break
}//Www.jb51.net
Mt_srand (Double) Microtime () *1000000*getmypid ());
while (strlen ($password) < $len) {
$tmp =substr ($chars, (Mt_rand ()%strlen ($chars)), 1);
if (($is _numer <> 1 && is_numeric ($tmp) && $tmp > 0) | | $format = = ' CHAR ') {
$is _numer = 1;
}
if ($is _abc <> 1 && preg_match ('/[a-za-z]/', $tmp)) | | $format = = ' number ') {
$is _abc = 1;
}
$password. = $tmp;
}
if ($is _numer <> 1 | | $is _abc <> 1 | | empty ($password)) {
$password = RANDPW ($len, $format);
}
return $password;
}
for ($i = 0; $i < $i + +) {
Echo RANDPW (8, ' number ');
echo "<br>";
}

Three ways to generate random numbers in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.