在网站功能建设中,php随机数是常用的一个功能,比如用户注册时随机产生的验证码、又比如
用户注册时生成随机的一组密码后可自行修改,当用户需要重置密码的时候也需要生成一个随机的密码······随机数就根据特定的规则(或在一个数组范围内)所自动产生的字符串,在php开发中应用广泛,不过php中的随机数也分好几种不同的情况,首先来认识一下PHP提供的随机数函数rand()。PHP的rand()函数将返回随机整数,具体使用方法如下:
rand(min,max)
可选参数min和max可以使rand() 返回0到RAND_MAX之间的伪随机整数。例如,想要5到15(包括 5 和 15)之间的随机数,用 rand(5, 15)。
下面我来看一个具体的示例,我们做一个基本的函数调用,不设置具体的参数,我们得到的随机数将不受min和max两个参数的限制。
echo(rand());?>
得到的结果:652696728 (随机结果)echo(rand(1000,2000));?>
够简单吧,下面来点稍微有些难度的。在本文的开始我们说过,随机数的作用很大,我们可以用PHP随机数解决一些复杂的问题。$my_array=array("ASP","PHP","JAVASCRIPT","AJAX","CSS","JQUERY","HTML");
echo($my_array[rand(0,6)]);
?>
可以想象,我们得到的结果可能是ASP、PHP或JavaScript等任何一个包含在数组中的元素。注意,我们的my_array数组包含七个元素,我们将rand()的参数设置为0到6之间。$my_array=array("ASP","PHP","JAVASCRIPT","AJAX","CSS","JQUERY","HTML");
$repetition=rand(0,6);
for($i=0;$i<=$repetition;$i++){
echo("I am learning " . $my_array[rand(0,6)]);
echo(" on .cn");
}
?>
我们得到的结果可能像下面这样:function genToken( $len = 32, $md5 = true ) {
# Seed random number generator
# Only needed for PHP versions prior to 4.2
mt_srand( (double)microtime()*1000000 );
# Array of characters, adjust as desired
$chars = array(
"Q", "@", "8", "y", "%", "^", "5", "Z", "(", "G", "_", "O", "`",
"S", "-", "N", "<", "D", "{", "}", "[", "]", "h", ";", "W", ".",
"/", "|", ":", "1", "E", "L", "4", "&", "6", "7", "#", "9", "a",
"A", "b", "B", "~", "C", "d", ">", "e", "2", "f", "P", "g", ")",
"?", "H", "i", "X", "U", "J", "k", "r", "l", "3", "t", "M", "n",
"=", "o", "+", "p", "F", "q", "!", "K", "R", "s", "c", "m", "T",
"v", "j", "u", "V", "w", ",", "x", "I", "$", "Y", "z", "*"
);
# Array indice friendly number of chars;
$numChars = count($chars) - 1; $token = "";
# Create random token at the specified length
for ( $i=0; $i<$len; $i++ )
$token .= $chars[ mt_rand(0, $numChars) ];
# Should token be run through md5?
if ( $md5 ) {
# Number of 32 char chunks
$chunks = ceil( strlen($token) / 32 ); $md5token = "";
# Run each chunk through md5
for ( $i=1; $i<=$chunks; $i++ )
$md5token .= md5( substr($token, $i * 32 - 32, 32) );
# Trim the token
$token = substr($md5token, 0, $len);
} return $token;
}
<?php
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 = "";
for($i=0; $i < count($arr);$i++)
{
$result .= $arr[$i].",";
}
$result = substr($result, 0, -1);
echo $result;
?>
运行结果:
2,3,4,6,7,8,9,10,11,12,13,16,20,21,22,24
$rand_array = array();
function get_rand_n($rand_array) {
$rand_number = mt_rand(1,9);
if(empty($rand_array)) {
$rand_array[] = $rand_number;
return get_rand_n($rand_array);
} else {
$count = 0;
foreach($rand_array as $item) {
$count += $item;
}
if($count<100) {
if($count+$rand_number == 100) {
$rand_array[] = $rand_number;
return $rand_array;
} else if($count+$rand_number < 100) {
$rand_array[] = $rand_number;
return get_rand_n($rand_array); // 回掉再次计算
} else { // 如果得到的值大于了100
return get_rand_n($rand_array); // 重新获得随机数,知道为100的时候返回这个随机数数组
}
}
}
}
$rand_array = get_rand_n($rand_array);
var_dump($rand_array);
运行结果:<?php
function GetfourStr($len)
{
$chars_array = array(
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z",
);
$charsLen = count($chars_array) - 1;
$outputstr = "";
for ($i=0; $i<$len; $i++)
{
$outputstr .= $chars_array[mt_rand(0, $charsLen)];
}
return $outputstr;
}
echo GetfourStr(4);
?>
如果没有提供可选参数 min 和 max,mt_rand() 返回 0 到 RAND_MAX 之间的伪随机数。例如想要 0 到 46(包括 0 和 46)之间的随机数,用 mt_rand(0, 46)。
<?php
function get_random($len=3){
//range 是将10到99列成一个数组
$numbers = range (10,99);
//shuffle 将数组顺序随即打乱
shuffle ($numbers);
//取值起始位置随机
$start = mt_rand(1,10);
//取从指定定位置开始的若干数
$result = array_slice($numbers,$start,$len);
$random = "";
for ($i=0;$i<$len;$i++){
$random = $random.$result[$i];
}
return $random;
}
//随机数
function get_random2($length = 4) {
$min = pow(10 , ($length - 1));
$max = pow(10, $length) - 1;
return mt_rand($min, $max);
}
echo "<br/>";
echo get_random(3);
echo "<br/>";
echo get_random2(6);
function create_password($pw_length = 8)
{
$randpwd = "";
for ($i = 0; $i < $pw_length; $i++)
{
$randpwd .= chr(mt_rand(33, 126));
}
return $randpwd;
}
// 调用该函数,传递长度参数$pw_length = 6
echo create_password(6);
function generate_password( $length = 8 ) {
// 密码字符集,可任意添加你需要的字符
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|";
$password = "";
for ( $i = 0; $i < $length; $i++ )
{
// 这里提供两种字符获取方式
// 第一种是使用 substr 截取$chars中的任意一位字符;
// 第二种是取字符数组 $chars 的任意元素
// $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
$password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
}
return $password;
}
function make_password( $length = 8 )
{
// 密码字符集,可任意添加你需要的字符
$chars = array("a", "b", "c", "d", "e", "f", "g", "h",
"i", "j", "k", "l","m", "n", "o", "p", "q", "r", "s",
"t", "u", "v", "w", "x", "y","z", "A", "B", "C", "D",
"E", "F", "G", "H", "I", "J", "K", "L","M", "N", "O",
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y","Z",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "!",
"@","#", "$", "%", "^", "&", "*", "(", ")", "-", "_",
"[", "]", "{", "}", "<", ">", "~", "`", "+", "=", ",",
".", ";", ":", "/", "?", "|");
// 在 $chars 中随机取 $length 个数组元素键名
$keys = ($chars, $length);
$password = "";
for($i = 0; $i < $length; $i++)
{
// 将 $length 个数组元素连接成字符串
$password .= $chars[$keys[$i]];
}
return $password;
}
<?php
function getmicrotime()
{
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
// 记录开始时间
$time_start = getmicrotime();
// 这里放要执行的PHP代码,如:
// echo create_password(6);
// 记录结束时间
$time_end = getmicrotime();
$time = $time_end - $time_start;
// 输出运行总时间
echo "执行时间 $time seconds";
?>
HTML属性元素可以通过其属性进行配置。以下代码显示了适用于a元素的属性。I like a href=/index.htmlCSS/a and HTML. a 元素允许...
HTML结构元素和属性用于标记HTML文档中的内容。HTML文档是具有.html文件扩展名的文本文件。您可以直接从磁盘或通过Web服务器将文...
HTML表单输入类型按钮输入输入元素的提交,重置和按钮类型创建与使用button元素时创建的按钮非常相似的按钮。创建按钮的输入元素...
HTML progressprogress元素可用于指示任务的逐渐完成。它有两个局部属性: value,max,form 。value属性定义当前进度,它在零和...
HTML 表单属性HTML表单属性Form autocomplete有时候你不想让浏览器自动填写表单。我们可以使用form元素中的autocomplete属性来控...