php 生成随机数字和字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7322689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generating a random numbers and letters
提问by glove
Can I ask for a certain code on how to generate a random letters and numbers in one word. I know there's a PHP function rand(), but I don't know if it's also applicable with letters. There's also a function called mt_rand() but I don't know how it works. I'm planning on generating a word like this one:
我可以要求有关如何在一个单词中生成随机字母和数字的特定代码吗?我知道有一个 PHP 函数 rand(),但我不知道它是否也适用于字母。还有一个名为 mt_rand() 的函数,但我不知道它是如何工作的。我打算生成一个这样的词:
$randomcode = re784dfg7ert7;
Do you guys have any simple code on this one? Thanks in advance!
大佬有没有简单的代码 提前致谢!
采纳答案by Zuul
This is a more extensive method that I use constantly to generate random numbers, letters or mixed:
这是一种更广泛的方法,我经常使用它来生成随机数、字母或混合:
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;
}
USAGE:
用法:
Basically I have a main function with the array, then I call secondary functions to build my string based on the length parameter:
基本上我有一个带有数组的主函数,然后我调用辅助函数来根据长度参数构建我的字符串:
Letters:
字母:
$str = get_rand_letters(8); // Only Letters
Numbers:
数字:
$str = get_rand_numbers(8); // Only Numbers
AlphaNumeric:
字母数字:
$str = get_rand_alphanumeric(8); // Numbers and Letters
This Question is answered by Kerrek SB, but this may help someone searching for a more extensive and flexible way.
这个问题由Kerrek SB回答,但这可能有助于寻找更广泛和灵活的方式的人。
回答by Tamilarasan
<?php
echo substr(sha1(mt_rand()),17,6); //To Generate Random Numbers with Letters.
?>
回答by Kerrek SB
Step 1: Create an alphabet, $alph = "0123456789abcde...";
.
第 1 步:创建一个字母表,$alph = "0123456789abcde...";
。
Step 2: Create a random number, $n = rand(0, ALPHSIZE-1);
, or use mt_rand()
.
第 2 步:创建一个随机数$n = rand(0, ALPHSIZE-1);
,或使用mt_rand()
。
Step 3: Get the appropriate index in the alphabet: $alph[n];
第 3 步:在字母表中获取适当的索引: $alph[n];
Rinse and repeat steps 2 and 3 for as many times as you need characters.
根据需要多次冲洗并重复步骤 2 和 3。
If you want strong statistical properties (like uniformness), you should work a little harder with the random number, but this should get you started.(I think the statistical properties of that should be sufficient.)
如果您想要强大的统计属性(如均匀性),您应该更努力地处理随机数,但这应该会让您开始。(我认为它的统计特性应该足够了。)
OK, might as well spell it out:
好吧,不妨把它拼出来:
$alph = "012...";
function make_random_string($N)
{
$s = "";
for ($i = 0; $i != $N; ++$i)
s .= $alph[mt_rand(0, ALPHSIZE - 1)];
return $s;
}
And here's the version that takes a custom alphabet:
这是采用自定义字母表的版本:
function make_random_custom_string($N, $alphabet)
{
$s = "";
for ($i = 0; $i != $N; ++$i)
s .= $alphabet[mt_rand(0, strlen($alphabet) - 1)];
return $s;
}
Example: 10 random odd digits: make_random_custom_string(10, "13579");
示例:10 个随机奇数: make_random_custom_string(10, "13579");
回答by brian_d
回答by brian_d
It might be done this way:
可以这样做:
function RandomCode($length = 10)
{
$code = '';
$total = 0;
do
{
if (rand(0, 1) == 0)
{
$code.= chr(rand(97, 122)); // ASCII code from **a(97)** to **z(122)**
}
else
{
$code.= rand(0, 9); // Numbers!!
}
$total++;
} while ($total < $length);
return $code;
}
回答by Chandler
Try this, it worked for me.
试试这个,它对我有用。
$numLenth = 35;
function make_seed_Token() {
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
srand(make_seed_Token());
$numSeed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$getNumber = "";
for($i = 0; $i < $numLenth; $i ++) {
$getNumber .= $numSeed[rand(0, strlen($numSeed))];
}
echo $getNumber;
回答by Virender Singh
Easiest way to do this with full control:
完全控制的最简单方法:
<?php
$str = 'QaR0SbT1UcV2WdX3YeZ4f5g6h7i8j9kAlBmCnDoEpFqGrHsItJuKvLwMxNyOzP';
// a-z 0-9 A-Z in above string
$shuffled = str_shuffle($str);
$shuffled = substr($shuffled,1,30);
echo $shuffled;
?>
Change $str according to your needs. I used small alphabets a to z, capital alphabets A to Z and numeric values 0 to 9.
根据您的需要更改 $str 。我使用了小字母 a 到 z、大写字母 A 到 Z 和数值 0 到 9。
回答by Asim Shahzad
What about this simple code. This will generate 10 characters,you can limit characters by modifying the limits (0,10)
这个简单的代码怎么样。这将生成 10 个字符,您可以通过修改限制 (0,10) 来限制字符
$str = substr(md5(time()), 0, 10);
echo $str; //7aca159655
回答by Matt
function randText($len=4){
$str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
for($i=0;$i<$len;$i++){
$txt.=substr($str, rand(0, strlen($str)), 1);
}
return $txt;
}
to use simply type:
使用简单的类型:
$yourVar = randText(10); // to get 10char long text
回答by SK Developers
This will create random alpha num string based on variable chars:
这将创建基于可变字符的随机字母数字字符串:
function createRandomCode()
{
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
srand((double) microtime() * 1000000);
$i = 0;
$pass = '';
while ($i <= 7)
{
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
Use this function as $random = createRandomCode();
将此功能用作 $random = createRandomCode();