PHP three ways to generate random numbers _php tutorial

Source: Internet
Author: User

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.

1

2 $arr =range (1,10);

3 Shuffle ($arr);

4 foreach ($arr as $values)

5 {

6 echo $values. " ";

7 }

8 ?>

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

1

2 $arr =array ();

3 while (count ($arr) <10)

4 {

5 $arr []=rand (1,10);

6 $arr =array_unique ($arr);

7 }

8 echo Implode ("", $arr);

9 ?>

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

01

02 $arr =array ();

03 $count 1=0;

04 $count = 0;

05 $return = Array ();

06 while ($count < 10)

07 {

08 $return [] = Mt_rand (1, 10);

09 $return = Array_flip (Array_flip ($return));

10 $count = count ($return);

11 }//www.jbxue.com

12 foreach ($return as $value)

13 {

14 echo $value. " ";

15 }

16 echo "
";

17 $arr =array_values ($return);//Get the value of the array

18 foreach ($arr as $key)

19 echo $key. " ";

20 ?>

PHP Random number Generation Function Example

01

02 function RANDPW ($len =8, $format = ' all ') {

03 $is _ABC = $is _numer = 0;

04 $password = $tmp = ";

05 Switch ($format) {

06 Case ' all ':

07 $chars = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ';

08 Break

09 Case ' CHAR ':

10 $chars = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz ';

11 Break

12 Case ' number ':

13 $chars = ' 0123456789 ';

14 Break

15 Default:

16 $chars = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ';

17 Break

18 }//Www.jbxue.com

19 Mt_srand (Double) Microtime () *1000000*getmypid ());

20 while (strlen ($password) < $len) {

21st $tmp =substr ($chars, (Mt_rand ()%strlen ($chars)), 1);

22 if (($is _numer <> 1 && is_numeric ($tmp) && $tmp > 0) | | $format = = ' CHAR ') {

23 $is _numer = 1;

24 }

25 if ($is _abc <> 1 && preg_match ('/[a-za-z]/', $tmp)) | | $format = = ' number ') {

26 $is _abc = 1;

27 }

28 $password. = $tmp;

29 }

30 if ($is _numer <> 1 | | $is _abc <> 1 | | empty ($password)) {

31 $password = RANDPW ($len, $format);

32 }

33 return $password;

34 }

35 for ($i = 0; $i < $i + +) {

36 Echo RANDPW (8, ' number ');

37 echo "
";

38 }

http://www.bkjia.com/PHPjc/876453.html www.bkjia.com true http://www.bkjia.com/PHPjc/876453.html techarticle Three ways PHP generates random numbers how do I generate a random number between 1-10 in PHP? Example 1, use the shuffle function to generate a random number. 1. PHP 2 $arr =range (1,10); 3 Shuffle ($arr); 4 fo ...

  • 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.