生成随机数字和字母

glo*_*ove 12 php

我可以要求一些代码来说明如何在一个单词中生成随机字母和数字.我知道有一个PHP函数rand(),但我不知道它是否也适用于字母.还有一个名为mt_rand()的函数,但我不知道它是如何工作的.我打算生成一个像这样的词:

$randomcode = re784dfg7ert7;
Run Code Online (Sandbox Code Playgroud)

你们这个人有什么简单的代码吗?提前致谢!

Zuu*_*uul 12

这是一种更广泛的方法,我经常使用它来生成随机数,字母或混合:

function assign_rand_value($num) {

    // accepts 1 - 36
    switch($num) {
        case "1"  : $rand_value = "a"; break;
        case "2"  : $rand_value = "b"; break;
        case "3"  : $rand_value = "c"; break;
        case "4"  : $rand_value = "d"; break;
        case "5"  : $rand_value = "e"; break;
        case "6"  : $rand_value = "f"; break;
        case "7"  : $rand_value = "g"; break;
        case "8"  : $rand_value = "h"; break;
        case "9"  : $rand_value = "i"; break;
        case "10" : $rand_value = "j"; break;
        case "11" : $rand_value = "k"; break;
        case "12" : $rand_value = "l"; break;
        case "13" : $rand_value = "m"; break;
        case "14" : $rand_value = "n"; break;
        case "15" : $rand_value = "o"; break;
        case "16" : $rand_value = "p"; break;
        case "17" : $rand_value = "q"; break;
        case "18" : $rand_value = "r"; break;
        case "19" : $rand_value = "s"; break;
        case "20" : $rand_value = "t"; break;
        case "21" : $rand_value = "u"; break;
        case "22" : $rand_value = "v"; break;
        case "23" : $rand_value = "w"; break;
        case "24" : $rand_value = "x"; break;
        case "25" : $rand_value = "y"; break;
        case "26" : $rand_value = "z"; break;
        case "27" : $rand_value = "0"; break;
        case "28" : $rand_value = "1"; break;
        case "29" : $rand_value = "2"; break;
        case "30" : $rand_value = "3"; break;
        case "31" : $rand_value = "4"; break;
        case "32" : $rand_value = "5"; break;
        case "33" : $rand_value = "6"; break;
        case "34" : $rand_value = "7"; break;
        case "35" : $rand_value = "8"; break;
        case "36" : $rand_value = "9"; break;
    }
    return $rand_value;
}

function get_rand_alphanumeric($length) {
    if ($length>0) {
        $rand_id="";
        for ($i=1; $i<=$length; $i++) {
            mt_srand((double)microtime() * 1000000);
            $num = mt_rand(1,36);
            $rand_id .= assign_rand_value($num);
        }
    }
    return $rand_id;
}

function get_rand_numbers($length) {
    if ($length>0) {
        $rand_id="";
        for($i=1; $i<=$length; $i++) {
            mt_srand((double)microtime() * 1000000);
            $num = mt_rand(27,36);
            $rand_id .= assign_rand_value($num);
        }
    }
    return $rand_id;
}

function get_rand_letters($length) {
    if ($length>0) {
        $rand_id="";
        for($i=1; $i<=$length; $i++) {
            mt_srand((double)microtime() * 1000000);
            $num = mt_rand(1,26);
            $rand_id .= assign_rand_value($num);
        }
    }
    return $rand_id;
}
Run Code Online (Sandbox Code Playgroud)

用法:

基本上我有一个数组的主函数,然后我调用辅助函数来构建我的字符串基于长度参数:

快报:

$str = get_rand_letters(8); // Only Letters
Run Code Online (Sandbox Code Playgroud)

编号:

$str = get_rand_numbers(8); // Only Numbers
Run Code Online (Sandbox Code Playgroud)

杬:

$str = get_rand_alphanumeric(8); // Numbers and Letters
Run Code Online (Sandbox Code Playgroud)

Kerrek SB回答了这个问题,但这可能有助于搜索更广泛和更灵活的方式.

  • 这真的不是“广泛”。这只是罗嗦。您可以将所有这些字母和数字填充到数组中并通过数字键引用它们。`assign_rand_values()` 似乎没有必要。此外,使用诸如“microtime()”之类的可预测数字进行播种并不总是最好的主意。(对不起,如果我听起来像个混蛋,我只是想提供帮助:)) (2认同)

小智 10

<?php
echo substr(sha1(mt_rand()),17,6); //To Generate Random Numbers with Letters.
?>
Run Code Online (Sandbox Code Playgroud)


Ker*_* SB 5

第1步:创建一个字母表,$alph = "0123456789abcde...";.

第2步:创建随机数$n = rand(0, ALPHSIZE-1);,或使用mt_rand().

第3步:在字母表中获取适当的索引: $alph[n];

根据需要冲洗并重复步骤2和3多次.

如果你想要强大的统计特性(比如统一性),你应该用随机数稍微努力一点,但这应该让你开始.(我认为它的统计特性应该足够了.)


好吧,不妨拼出来:

$alph = "012...";
function make_random_string($N)
{
  $s = "";
  for ($i = 0; $i != $N; ++$i)
    s .= $alph[mt_rand(0, ALPHSIZE - 1)];
  return $s;
}
Run Code Online (Sandbox Code Playgroud)

这是采用自定义字母表的版本:

function make_random_custom_string($N, $alphabet)
{
  $s = "";
  for ($i = 0; $i != $N; ++$i)
    s .= $alphabet[mt_rand(0, strlen($alphabet) - 1)];
  return $s;
}
Run Code Online (Sandbox Code Playgroud)

示例:10个随机奇数: make_random_custom_string(10, "13579");


bri*_*n_d 5

使用uniqid

$desired_length = 10; //or whatever length you want
$unique = uniqid();

$your_random_word = substr($unique, 0, $desired_length);
Run Code Online (Sandbox Code Playgroud)