Php 生成随机数字和字母
我可以问一个关于如何在一个单词中生成随机字母和数字的特定代码吗。我知道有一个PHP函数rand(),但我不知道它是否也适用于字母。还有一个名为mt_rand()的函数,但我不知道它是如何工作的。我计划生成这样一个词:Php 生成随机数字和字母,php,Php,我可以问一个关于如何在一个单词中生成随机字母和数字的特定代码吗。我知道有一个PHP函数rand(),但我不知道它是否也适用于字母。还有一个名为mt_rand()的函数,但我不知道它是如何工作的。我计划生成这样一个词: $randomcode = re784dfg7ert7; 你们有关于这个的简单代码吗?提前谢谢 步骤1:创建字母表,$alph=“0123456789abcde…” 步骤2:创建一个随机数,$n=rand(0,ALPHSIZE-1),或使用mt\u rand() 步骤3:获取字母
$randomcode = re784dfg7ert7;
你们有关于这个的简单代码吗?提前谢谢 步骤1:创建字母表,
$alph=“0123456789abcde…”代码>
步骤2:创建一个随机数,$n=rand(0,ALPHSIZE-1)代码>,或使用mt\u 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;
}
以下是采用自定义字母表的版本:
function make_random_custom_string($N, $alphabet)
{
$s = "";
for ($i = 0; $i != $N; ++$i)
s .= $alphabet[mt_rand(0, strlen($alphabet) - 1)];
return $s;
}
示例:10个随机奇数:make_random_custom_string(10,“13579”)代码>使用
这是一种更广泛的方法,我经常使用它来生成随机数、字母或混合数:
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;
}
数字:
字母数字:
Kerrek SB回答了这个问题,但这可能有助于人们寻找更广泛、更灵活的方法。可以这样做:
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;
}
函数随机码($length=10)
{
$code='';
$total=0;
做
{
如果(rand(0,1)=0)
{
$code.=chr(rand(97122));//从**a(97)**到**z(122)的ASCII码**
}
其他的
{
$code.=rand(0,9);//数字!!
}
$total++;
}而($总长度<$长度);
返回$code;
}
我创建了一个PHP类,用于生成随机数和字符串
它使用“mcrypt_create_iv(4,mcrypt_DEV_uradom)”生成随机数和值。我是在一个加密项目工作时做的,因为我需要一个安全的随机值生成器,而mt_rand()不满足这个要求。下面是一个示例用法
$randomValue = new RandomValue;
$randomValue->randomNumber(): = -3880998
$randomValue->randomNumberBetween(1,10): = 2
$randomValue->randomTextString(): = CfCkKDHRgUULdGWcSqP4
$randomValue->randomTextString(10): = LorPIxaeEY
$randomValue->randomKey(): = C7al8tX9.gqYLf2ImVt/!$NOY79T5sNCT/6Q.$!.6Gf/Q5zpa3
$randomValue->randomKey(10): = RDV.dc6Ai/
这对我来说是一个简单的方法-生成一个15个字符的字符串-
$alph = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$code='';
for($i=0;$i<15;$i++){
$code .= $alph[rand(0, 35)];
}
$alph=“0123456789abcdefghijklmnopqrstuvxyz”;
$code='';
对于($i=0;$i试试这个,它对我有用
$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;
$numLenth=35;
函数make_seed_Token(){
列表($usec,$sec)=爆炸('',微时间());
收益(浮动)$sec+((浮动)$usec*100000);
}
srand(make_seed_Token());
$numsed=“abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyzo123456789”;
$getNumber=“”;
对于($i=0;$i<$numLenth;$i++){
$getNumber.=$numsed[rand(0,strlen($numsed))];
}
echo$getNumber;
实现完全控制的最简单方法:
<?php
$str = 'QaR0SbT1UcV2WdX3YeZ4f5g6h7i8j9kAlBmCnDoEpFqGrHsItJuKvLwMxNyOzP';
// a-z 0-9 A-Z in above string
$shuffled = str_shuffle($str);
$shuffled = substr($shuffled,1,30);
echo $shuffled;
?>
根据需要更改$str。我使用了小字母a到z、大写字母a到z以及数字值0到9。//一个包含随机字母和数字的字符串。a-z、a-z、0-9
// A string with random letters and numbers. A-Z, a-z, 0-9
// A function in PHP is a block of code that can be used elsewhere in code.
// This function is called rand_string and will generate a random sequence of characters in one string.
// A default value of 16 characters is set, so that if no integer is supplied it will use the value of 16.
function rand_string($length = 16) {
// A string is something that holds alphanumeric characters and other symbols.
// This string is an empty one, or at least that's how it starts.
$string = '';
// This is known as a for/next loop, it's will run a section of code for a set number of times.
// A counter $i is incremented on each pass. In this case until it has operated $length number of times.
for ($i = 0; $i < $length; $i++) {
// This variable ($die) is assigned a random number - which is obtained via the PHP function mt_rand.
//Consult the PHP docs for more information.
$die = mt_rand(1, 3);
// This switch statement picks a case that is true and runs the accompanying code as defined in each case.
switch ($die) {
// This case will be activated if the variable $die has the value of 1. And case 2 if it has the value of 2 and so on.
case 1:
// Here and subsequently a random value between 48 and 57 is assigned to the $rnd variable.
$rnd = mt_rand(48, 57);
break;
case 2:
$rnd = mt_rand(65, 90);
break;
case 3:
$rnd = mt_rand(97, 122);
break;
}
// This is another variable $string which is assigned the ASCII character that is represented by the $rnd variable.
// ASCII characters are codes that computers use to represent characters and symbols.
// The chr function is a special PHP function that returns the character represented by the ASCII code.
// In this case the value of $rnd.
$string .= chr($rnd);
}
// Here we reach the final result. The value of $string is returned to source of the function call.
return $string;
}
// Segments of the function, loops and switches are enclosed between curly brackets {}. This limits the scope of the processing contained within.
// Usage of this function to obtain a 10 character random string.
// echo is a function that prints the result to the browser/screen.
$mystring = rand_string(10);
echo $mystring;
//PHP中的函数是可以在代码中的其他地方使用的代码块。
//此函数称为rand_string,将在一个字符串中生成随机字符序列。
//设置了16个字符的默认值,因此如果未提供整数,则将使用16的值。
函数rand_字符串($length=16){
//字符串是保存字母数字字符和其他符号的东西。
//这个字符串是空的,或者至少它是这样开始的。
$string='';
//这就是所谓的for/next循环,它将运行一段代码一定次数。
//计数器$i在每次传递时递增。在这种情况下,直到它运行$length多次。
对于($i=0;$i<$length;$i++){
//这个变量($die)被分配了一个随机数,这个随机数是通过PHP函数mt_rand获得的。
//有关更多信息,请参阅PHP文档。
$die=百万兰特(1,3);
//这个switch语句选择一个为true的案例,并按照每个案例中的定义运行附带的代码。
开关($die){
//如果变量$die的值为1,则激活此案例。如果变量$die的值为2,则激活案例2,依此类推。
案例1:
//此处以及随后将48和57之间的随机值分配给$rnd变量。
$rnd=百万兰特(48,57);
打破
案例2:
$rnd=百万兰特(65,90);
打破
案例3:
$rnd=百万兰特(97122);
打破
}
//这是另一个变量$string,它被分配了由$rnd变量表示的ASCII字符。
//ASCII字符是计算机用来表示字符和符号的代码。
//chr函数是一个特殊的PHP函数,返回ASCII码表示的字符。
//在这种情况下,价值为$rnd。
$string.=chr($rnd);
}
//在这里我们得到了最终结果,$string的值被返回到函数调用的源。
返回$string;
}
//函数、循环和开关的段被括在花括号{}之间。这限制了其中包含的处理范围。
//使用此函数可获得10个字符的随机字符串。
//echo是一种将结果打印到浏览器/屏幕的功能。
$mystring=rand_字符串(10);
echo$mystring;
这个简单的代码怎么样。
这将生成10个字符,您可以通过修改限制(0,10)来限制字符
这将基于变量字符创建随机alpha num字符串:
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;
}
函数createRandomCode()
{
$chars=“abcdefghijkmnopqrstuvxyzo23456789”;
srand((双)微时间()*1000000);
$i=0;
$pass='';
而($irand()%N
更喜欢c
$alph = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$code='';
for($i=0;$i<15;$i++){
$code .= $alph[rand(0, 35)];
}
$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;
<?php
$str = 'QaR0SbT1UcV2WdX3YeZ4f5g6h7i8j9kAlBmCnDoEpFqGrHsItJuKvLwMxNyOzP';
// a-z 0-9 A-Z in above string
$shuffled = str_shuffle($str);
$shuffled = substr($shuffled,1,30);
echo $shuffled;
?>
// A string with random letters and numbers. A-Z, a-z, 0-9
// A function in PHP is a block of code that can be used elsewhere in code.
// This function is called rand_string and will generate a random sequence of characters in one string.
// A default value of 16 characters is set, so that if no integer is supplied it will use the value of 16.
function rand_string($length = 16) {
// A string is something that holds alphanumeric characters and other symbols.
// This string is an empty one, or at least that's how it starts.
$string = '';
// This is known as a for/next loop, it's will run a section of code for a set number of times.
// A counter $i is incremented on each pass. In this case until it has operated $length number of times.
for ($i = 0; $i < $length; $i++) {
// This variable ($die) is assigned a random number - which is obtained via the PHP function mt_rand.
//Consult the PHP docs for more information.
$die = mt_rand(1, 3);
// This switch statement picks a case that is true and runs the accompanying code as defined in each case.
switch ($die) {
// This case will be activated if the variable $die has the value of 1. And case 2 if it has the value of 2 and so on.
case 1:
// Here and subsequently a random value between 48 and 57 is assigned to the $rnd variable.
$rnd = mt_rand(48, 57);
break;
case 2:
$rnd = mt_rand(65, 90);
break;
case 3:
$rnd = mt_rand(97, 122);
break;
}
// This is another variable $string which is assigned the ASCII character that is represented by the $rnd variable.
// ASCII characters are codes that computers use to represent characters and symbols.
// The chr function is a special PHP function that returns the character represented by the ASCII code.
// In this case the value of $rnd.
$string .= chr($rnd);
}
// Here we reach the final result. The value of $string is returned to source of the function call.
return $string;
}
// Segments of the function, loops and switches are enclosed between curly brackets {}. This limits the scope of the processing contained within.
// Usage of this function to obtain a 10 character random string.
// echo is a function that prints the result to the browser/screen.
$mystring = rand_string(10);
echo $mystring;
$str = substr(md5(time()), 0, 10);
echo $str; //7aca159655
echo bin2hex(openssl_random_pseudo_bytes(12));
function randText($len=4){
$str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
for($i=0;$i<$len;$i++){
$txt.=substr($str, rand(0, strlen($str)), 1);
}
return $txt;
}
$yourVar = randText(10); // to get 10char long text
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;
}