php 随机数
php mt_srand 播下一个更好的随机数发生器种子
mt_srand
(PHP 3 >= 3.0.6, PHP 4, PHP 5)
mt_srand -- 播下一个更好的随机数发生器种子
说明
void mt_srand ( int seed )
用 seed 来给随机数发生器播种。从 PHP 4.2.0 版开始,seed 参数变为可选项,当该项为空时,会被设为随时数。
例子 1. mt_srand() 范例
代码
php
// seed with microseconds
function make_seed()
{
list ( $usec , $sec ) = explode ( ' ' , microtime ());
return ( float ) $sec + (( float ) $usec * 100000 );
}
mt_srand (make_seed());
$randval = mt_rand ();
?>
注: 自 PHP 4.2.0 起,不再需要用 srand() 或 mt_srand() 函数给随机数发生器播种,现已自动完成。
参见 mt_rand(),mt_getrandmax() 和 srand()。
PHP mt_rand() 函数
定义和用法
mt_rand() 使用 Mersenne Twister 算法返回随机整数。
语法<p class="sycode"> <p class="sycode"> mt_rand ( min , max ) </p> </p><p> </p>
如果没有提供可选参数 min 和 max,mt_rand() 返回 0 到 RAND_MAX 之间的伪随机数。例如想要 5 到 15(包括 5 和 15)之间的随机数,用 mt_rand(5, 15)。
很多老的 libc 的随机数发生器具有一些不确定和未知的特性而且很慢。PHP 的 rand() 函数默认使用 libc 随机数发生器。mt_rand() 函数是非正式用来替换它的。该函数用了 Mersenne Twister 中已知的特性作为随机数发生器,它可以产生随机数值的平均速度比 libc 提供的 rand() 快四倍。
提示和注释
注释:自 PHP 4.2.0 起,不再需要用 srand() 或 mt_srand() 函数给随机数发生器播种,现在已自动完成。
注释:在 3.0.7 之前的版本中,max 的含义是 range 。要在这些版本中得到和上例相同 5 到 15 的随机数,简短的例子是 mt_rand (5, 11)。
例子
在本例中,我们会返回一些随机数:
<p class="sycode"> <p class="sycode"> <? php echo ( mt_rand ()); echo ( mt_rand ()); echo ( mt_rand ( 10 , 100 )); ?> </p> </p><p> </p>
输出类似:
<p class="sycode"> <p class="sycode"> 3150906288 513289678 35 </p> </p><p> </p><p> </p><p></p><p> </p>
注意:上面这个函数给出的随机整数,数字之外的字符是不会出来的,若想其他字符产生,就需另自定义方法,如下所示:
代码
php
/*
* $length:随机数字符串的长度
* $type:产生随机数的类型
* */
function random( $length , $type = "" ) {
$chars = ! $type ? " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz " : " 0123456789abcdef " ;
$max = strlen ( $chars ) - 1 ;
mt_srand (( double ) microtime () * 1000000 );
for ( $i = 0 ; $i $string .= $chars [ mt_rand ( 0 , $max )];
}
return $string ;
}
$var = random( 32 , ' haha ' );
echo ( $var );
?>
输出:
fe61e294e5f46437cb3a92b92643ead6

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Alipay PHP...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.
