Home  >  Article  >  Backend Development  >  Detailed explanation of the steps to generate a random string of custom length in PHP

Detailed explanation of the steps to generate a random string of custom length in PHP

php中世界最好的语言
php中世界最好的语言Original
2018-05-21 11:31:271721browse

This time I will bring you a detailed explanation of the steps to generate a random string with a custom length in php. What are the precautions for generating a random string with a custom length in php? The following is a practical case. , let’s take a look.

//随机生成字符串
function random($length) {
     srand(date("s"));
     $possible_charactors = "0123456789abcdefghijklmnopqrstuvwxyz";
     $string = "";
     while(strlen($string)<$length) {
          $string .= substr($possible_charactors,(rand()%(strlen($possible_charactors))),1);
     }
     return($string);
}

Example 2, php generates n non-repeating random number instances

Use php to generate n non-repeating random number instances

There are 25 works for voting. You need to select 16 works in one vote, and a single work can only be selected once in one vote.

A programmer made a mistake earlier and forgot to store the votes in the database. The voting sequences generated by 200 users were empty. So how do you fill this gap?
Need to generate 16 non-repeating random numbers between 1-25 to fill. How to design the function specifically? Store random numbers in an array, and then remove duplicate values ​​in the array to generate a certain number of non-repeating random numbers.
Program:

<?php
/*
* array unique_rand( int $min, int $max, int $num )
* 生成一定数量的不重复随机数
* $min 和 $max: 指定随机数的范围
* $num: 指定生成数量
*/ www.jbxue.com
function unique_rand($min, $max, $num) {
    $count = 0;
    $return = array();
    while ($count < $num) {
        $return[] = mt_rand($min, $max);
        $return = array_flip(array_flip($return));
        $count = count($return);
    }
    shuffle($return);
    return $return;
}
$arr = unique_rand(1, 25, 16);
sort($arr);
$result = &#39;&#39;;
for($i=0; $i < count($arr);$i++)
{
 $result .= $arr[$i].&#39;,&#39;;
}
$result = substr($result, 0, -1);
echo $result;
?>
Program running:

2,3,4,6,7,8,9,10,11,12,13,16,20,21,22,24

Supplement:

The mt_rand() function is used to generate random numbers. This function generates random numbers four times faster on average than rand().
The "flip method" is used to remove duplicate values ​​in the array, which is to use array_flip() to exchange the key and value of the array twice. This approach is much faster than using
array_unique(). Before returning the array, first use shuffle() to assign new key names to the array, ensuring that the key names are consecutive numbers from 0-n. If this step is not performed, the key names may be discontinuous when
deleting duplicate values, causing trouble for traversal.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

php Detailed explanation of the steps for ZIP compression class example

Detailed explanation of the steps for PHP to determine the running status of the program

The above is the detailed content of Detailed explanation of the steps to generate a random string of custom length in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn