如何在不使用rand()函数的情况下获取固定长度的字符串

时间:2016-04-27 10:06:03

标签: php css

如何在不使用rand()函数的情况下获取固定长度的字符串?

我有这个,但我不想使用rand()函数

function generateRandomString($length =6) {
    $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';

    for ($j = 0; $j < $length; $j++) {
        $randomString .= $characters[mt_rand(0, $charactersLength - 1)];
    }

  return $randomString;
 }

3 个答案:

答案 0 :(得分:2)

<?php
$start=0;$length=6;
$str='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$Val=substr(str_shuffle($str),$start,$length);
echo $Val;
?>  

答案 1 :(得分:1)

arr[:,arr[1,:].argsort()]


在这里你可以调用这个函数为8个字符

SELECT c.name,(select count(*) from article where cid = c.id) as count  FROM cat c WHERE 1 GROUP BY c.id 

答案 2 :(得分:0)

您可以使用以下代码来获取固定长度的随机字符串

<?php
$str = 'abcdef';
$shuffled = str_shuffle($str);

 // this will genrate randome string with fixed string lenght
echo $shuffled;
?>