rand() - 产生一个随机整数 - php 数学函数
rand()
(PHP 4, PHP 5, PHP 7)
产生一个随机整数
说明
rand(void): intrand(int $min,int $max): int如果没有提供可选参数$min和$max,rand()返回 0 到getrandmax()之间的伪随机整数。例如想要 5 到 15(包括 5 和 15)之间的随机数,用rand(5, 15)。
Note:在某些平台下(例如 Windows)getrandmax()只有 32767。如果需要的范围大于 32767,那么指定$min和$max参数就可以生成更大的数了,或者考虑用mt_rand()来替代之。
参数
$min返回的最低值(默认:0)
$max返回的最高值(默认:getrandmax())
返回值
A pseudo random value between$min(or 0)and$max(orgetrandmax(), inclusive).
更新日志
版本 | 说明 |
---|---|
4.2.0 | 随机数发生器自动进行播种。 |
范例
Example #1rand()例子
以上例程的输出类似于:
7771 22264 11
参见
srand()
播下随机数发生器种子getrandmax()
显示随机数最大的可能值mt_rand()
生成更好的随机数
quick way to generate randomish numbers and simple strings. no messing around with functions, so you can just pop the line into the middle of your existing code. not the most perfect for sure, but ok for plenty of situations... hope someone finds it useful for somthing. regards, deeeeeen alxndr0u
I also enjoy making one-liners. Here's a non-regular expression approach. It generates a random 32 character string consisting of, by default, only A-Z, a-z, and 0-9, but you can change the value of $a for other characters. The random string will be in variable $s after this line. If you don't want the same character to appear beside itself, use this: For those of you who want both as a function, use this: string $c is the string of characters to use. integer $l is how long you want the string to be. boolean $u is whether or not a character can appear beside itself. Examples: rand_chars("ABCEDFG", 10) == GABGFFGCDA rand_chars("ABCEDFG", 10, TRUE) == CBGFAEDFEC
Here's an interesting note about the inferiority of the rand() function. Try, for example, the following code... which produces something similar to the following output (on my windows box, where RAND_MAX is 32768): Array ( [0] => 31 [1] => 0 [2] => 0 [3] => 31 [4] => 0 [5] => 0 [6] => 30 [7] => 0 [8] => 0 [9] => 31 [10] => 0 ) Within this range only multiples of 3 are being selected. Also note that values that are filled are always 30 or 31 (no other values! really!) Now replace rand() with mt_rand() and see the difference... Array ( [0] => 8 [1] => 8 [2] => 14 [3] => 16 [4] => 9 [5] => 11 [6] => 8 [7] => 9 [8] => 7 [9] => 7 [10] => 9 ) Much more randomly distributed! Conclusion: mt_rand() is not just faster, it is a far superior algorithm.
Since many people (myself included) come to this page looking for a way to do a random string, I present a way that uses arrays and shuffle() instead of rand(). This also has the effect of not repeating any characters in the value set. $arr = str_split('ABCDEFGHIJKLMNOP'); // get all the characters into an array shuffle($arr); // randomize the array $arr = array_slice($arr, 0, 6); // get the first six (random) characters out $str = implode('', $arr); // smush them back into a string
A small comment on phpdev-dunnbypauls conclusion that rand() only generates numbers that are a multiply of 3. Since, 100000/32768=3.05 you get multiples of 3. The random integer will be multiplied by 3.05 to fit between 0 and 100000. rand() works fine, if you don't ask for bigger numbers then RAND_MAX.
If you are looking for generate a random expression, like password with alphanumeric or any other character, use this function:
I agree with Sebmil (http://php.net/manual/en/function.array-rand.php#105265) that "array_rand()" produces weird and very uneven random distribution (as of my local PHP 5.3.8 and my public host's PHP 5.2.17). Unfortunately, I haven't got any access either to a server with the latest PHP version. My info is for those of you who like to check things for themselves and who don't believe all of the official statements in the docs. I've made a simple adjustment of his test code like this: And it appears to me that simple "$idx=rand(0, count($test)-1);" is much better than "$idx=array_rand($test, 1);". And what's more the simpler and a bit slower (0 ms up to total 712.357 ms at 5 mln cycles) "rand()" is better than "mt_rand()" in simple everyday use cases because it is more evenly distributed (difference least vs. most often numbers: ca. 0.20-1.28 % for "rand()" vs. ca. 1.43-1.68 % for "mt_rand()"). Try it for yourself... although it depends on your software and hardware configuration, range of numbers to choose from (due to random patterns), number of cycles in the loop, and temporary (public) server load as well.
Generate a random 5 character A-Z0-9 string # php -r 'for ($i=0; $i
Here's a simple function to generate a random date between a start date and an end date. It is inclusive of BOTH dates - so using dates 2009-04-01 and 2009-04-03 would generate a random date that could be 2009-04-01, 2009-04-02 or 2009-04-03. It won't work if the end date is prior to the start date and if you use a non-existant date (eg 2009-02-30) it defaults to 1970-01-01 the longer version: and the one-line version for compactness freaks: it is called like this Hope this is of some use to someone
Another one-liner to generate strings: The strings can be repeated to have the possibility that a character appears multiple times.
As an further optimization on janoserki[at]gmail[dot]com previous post i would recommend that you optimize you first part of php/sql code to something like this. the count(*) is much faster for the database than grabbing the hole dataset from the table.
Easy way for mysql: random row the original form is: "... order by rand()" but this is not the best way, because it's very slow by a big database (it can take more minutes to complete the request!) My suggestion:
Much easier way to generate random string of numbers and letters: This generates strings of about 11 characters. Experiment with the range for rand() if you want shorter or longer.
well , this is my vision about "custom random string" :
Lately I needed some random numbers with a gaussian (normal) distribution, not evenly distributed as the numbers generated by rand(). After googling a while, I found out that there is no perfect algrorithm that creates such numbers out of evenly distruted random numbers but a few methods that have similar effect. The following function implements all three algorithms I found- The the last two methods create numbers where you can find a lower and upper boundary and the first one will create a number from time to time (such as one in every 10000) that may be very far from the average value. Have fun testing and using it.
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)