Hướng dẫn range a-z php - phạm vi a-z php
(Php 4, Php 5, Php 7, Php 8) RAND - Tạo giá trị ngẫu nhiên — Génère une valeur aléatoire Rand (): int(): int Rand (int $ min, int $ max): int(int $min, int $max): int Được gọi là không có các tùy chọn tối đa và tối đa, rand () trả về số giả giữa 0 và getrandmax (). Nếu bạn muốn một số ngẫu nhiên trong khoảng từ 5 đến 15 (bao gồm), ví dụ, sử dụng rand (5, 15).rand() retourne un nombre pseudoaléatoire entre 0 et
getrandmax(). Si vous voulez un nombre aléatoire entre 5 et 15 (inclus), par exemple, utilisez rand (5, 15). Chú ý Hàm này không tạo ra các giá trị an toàn từ quan điểm mật mã và không nên được sử dụng trong bối cảnh mã hóa. Nếu bạn cần một giá trị an toàn từ quan điểm mật mã, hãy sử dụng Random_int (), Random_Bytes () hoặc OpenSSL_Random_Pseudo_Bytes ().random_int(), random_bytes(), ou
openssl_random_pseudo_bytes(). Lưu ý: Trên một số nền tảng (ví dụ: Windows), MT_GetRandMax () bị giới hạn ở 32767. Nếu bạn muốn giới hạn lớn hơn 32767, chỉ định Min và Max, bạn sẽ được phép sử dụng khoảng thời gian lớn hơn MT_GetRandma (), hoặc, sử dụng hàm mt_rand () thay thế.: Sur quelques plates-formes (par exemple, Windows), mt_getrandmax()est limité à 32767. Si vous désirez une limite supérieure à 32767, en spécifiant min et max, vous serez autorisés à utiliser un intervalle plus grand que mt_getrandmax(), ou bien, utilisez la fonction mt_rand() à la place. Lưu ý: Từ Php 7.1.0, rand () sử dụng cùng một trình tạo số ngẫu nhiên như mt_rand (). Để bảo tồn khả năng tương thích tăng dần, rand () cho phép tối đa nhỏ hơn phút so với lợi nhuận sai của mt_rand (): À partir de
php 7.1.0, rand() utilise le même générateur de nombres aléatoires que mt_rand(). Pour préserver la compatibilité ascendante, rand() permet à max d'être plus petit que min par opposition au retour false de mt_rand() Giá trị nhỏ nhất để trả về (theo mặc định, 0) Giá trị lớn nhất được trả về (mặc định, mt_getrandmax ())mt_getrandmax()) Giá trị giả hiệu quả, giữa min (hoặc 0) và tối đa (hoặc mt_getrandmax (), bao gồm).mt_getrandmax(), inclusif). Chú ýrand() Hàm này không tạo ra các giá trị an toàn từ quan điểm mật mã và không nên được sử dụng trong bối cảnh mã hóa. Nếu bạn cần một giá trị an toàn từ quan điểm mật mã, hãy sử dụng Random_int (), Random_Bytes () hoặc OpenSSL_Random_Pseudo_Bytes (). Lưu ý: Trên một số nền tảng (ví dụ: Windows), MT_GetRandMax () bị giới hạn ở 32767. Nếu bạn muốn giới hạn lớn hơn 32767, chỉ định Min và Max, bạn sẽ được phép sử dụng khoảng thời gian lớn hơn MT_GetRandma (), hoặc, sử dụng hàm mt_rand () thay thế. Lưu ý: Từ Php 7.1.0, rand () sử dụng cùng một trình tạo số ngẫu nhiên như mt_rand (). Để bảo tồn khả năng tương thích tăng dần, rand () cho phép tối đa nhỏ hơn phút so với lợi nhuận sai của mt_rand () Tối thiểu Giá trị nhỏ nhất để trả về (theo mặc định, 0)getrandmax(). i.e. (max - min) <= getrandmax() sinon, rand() peut retourner des nombres aléatoires de mauvaise qualité. Rand () đã được tạo thành một bí danh của mt_rand (). ¶ Ví dụ Ví dụ #1 Ví dụ với Rand () 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. tiếng vang Kết quả của ví dụ trên tương tự như: Hàm này không tạo ra các giá trị an toàn từ quan điểm mật mã và không nên được sử dụng trong bối cảnh mã hóa. Nếu bạn cần một giá trị an toàn từ quan điểm mật mã, hãy sử dụng Random_int (), Random_Bytes () hoặc OpenSSL_Random_Pseudo_Bytes (). Lưu ý: Trên một số nền tảng (ví dụ: Windows), MT_GetRandMax () bị giới hạn ở 32767. Nếu bạn muốn giới hạn lớn hơn 32767, chỉ định Min và Max, bạn sẽ được phép sử dụng khoảng thời gian lớn hơn MT_GetRandma (), hoặc, sử dụng hàm mt_rand () thay thế. Lưu ý: Từ Php 7.1.0, rand () sử dụng cùng một trình tạo số ngẫu nhiên như mt_rand (). Để bảo tồn khả năng tương thích tăng dần, rand () cho phép tối đa nhỏ hơn phút so với lợi nhuận sai của mt_rand () ¶ Danh sách cài đặt Tối thiểu Giá trị nhỏ nhất để trả về (theo mặc định, 0) Tối đa ¶ Ví dụ Ví dụ #1 Ví dụ với Rand () // estimate the number of rows in a
table tiếng vang ¶ Kết quả của ví dụ trên tương tự như: Ghi chú Cảnh báo Bãi biển Min Max phải ở Bãi biển Getrandmax (). tức là (tối đa - tối thiểu) ¶ Xem là tốt Srand () - Khởi tạo trình tạo số ngẫu nhiên GetRandMax () - Giá trị ngẫu nhiên có thể lớn hơn có thể jont tại live dot co dot uk ¶ ¶ 15 năm trước Đây không phải là một cách đơn giản hơn để tạo ID ngẫu nhiên cho một cái gì đó? Ý tôi là tôi biết rằng có một cơ hội rất nhỏ mà một bản sao có thể được thực hiện nhưng đó là một cơ hội rất, rất nhỏ, gần như không thể. $ rand = mt_rand (0, 32); $ code = md5 ($ rand. Time ()); echo "$ code"; Và nếu bạn không muốn nó, MD5 có thể bị xóa, tôi chỉ cần thêm nó như một sở thích ở đó :) Jon Janoserki [at] gmail [dot] com ¶
¶ 13 năm trước Cách dễ dàng cho MySQL: Rand ngẫu nhiên Row Biểu mẫu gốc là: "... Đặt hàng by rand ()" nhưng đây không phải là cách tốt nhất, bởi vì nó rất chậm bởi một cơ sở dữ liệu lớn (có thể mất nhiều phút hơn để hoàn thành yêu cầu!) gợi ý: // estimate the number of rows in a table
Ishtar ¶ ¶ 14 năm trước Một nhận xét nhỏ về kết luận của PHPDEV-Dunnbypauls rằng rand () chỉ tạo ra các số có số lượng 3. Vì, 100000/32768 = 3.05 Bạn nhận được bội số của 3. Số nguyên ngẫu nhiên sẽ được nhân với 3.05 để phù hợp giữa 0 đến 100000. rand () hoạt động tốt, nếu bạn không yêu cầu số lượng lớn hơn thì rand_max. PHPDEV tại Dunnbypaul dot net ¶ ¶ 15 năm trước Đây là một lưu ý thú vị về sự thấp kém của hàm rand (). Ví dụ, thử mã sau ... $r =
array(0,0,0,0,0,0,0,0,0,0,0); Ishtar ¶ 14 năm trước Một nhận xét nhỏ về kết luận của PHPDEV-Dunnbypauls rằng rand () chỉ tạo ra các số có số lượng 3. Vì, 100000/32768 = 3.05 Bạn nhận được bội số của 3. Số nguyên ngẫu nhiên sẽ được nhân với 3.05 để phù hợp giữa 0 đến 100000. rand () hoạt động tốt, nếu bạn không yêu cầu số lượng lớn hơn thì rand_max. PHPDEV tại Dunnbypaul dot net ¶ Đây là một lưu ý thú vị về sự thấp kém của hàm rand (). Ví dụ, thử mã sau ... Mảng (& nbsp; & nbsp; [0] => 31 & nbsp; & nbsp; [1] => 0 & nbsp; & nbsp; [2] => 0 & nbsp; & nbsp; [3] => 31 & nbsp; & nbsp; [4] ; [5] => 0 & nbsp; & nbsp; [6] => 30 & nbsp; & nbsp; [7] => 0 & nbsp; & nbsp; > 0) Trong phạm vi này chỉ có bội số của 3 đang được chọn. Cũng lưu ý rằng các giá trị được điền luôn luôn là 30 hoặc 31 (không có giá trị nào khác! Thực sự!) ¶ Bây giờ hãy thay thế rand () bằng mt_rand () và xem sự khác biệt ... Mảng (& nbsp; & nbsp; [0] => 8 & nbsp; & nbsp; [1] => 8 & nbsp; & nbsp; [2] => 14 & nbsp; & nbsp; [3] => 16 & nbsp; & nbsp; ; [5] => 11 & nbsp; & nbsp; [6] => 8 & nbsp; & nbsp; [7] => 9 & nbsp; & nbsp; > 9) Phân phối ngẫu nhiên hơn nhiều! ¶ 13 năm trước Cách dễ dàng cho MySQL: Rand ngẫu nhiên Row Biểu mẫu gốc là: "... Đặt hàng by rand ()" nhưng đây không phải là cách tốt nhất, bởi vì nó rất chậm bởi một cơ sở dữ liệu lớn (có thể mất nhiều phút hơn để hoàn thành yêu cầu!) gợi ý: // Generate a random character string Ishtar ¶ ¶ 14 năm trước Một nhận xét nhỏ về kết luận của PHPDEV-Dunnbypauls rằng rand () chỉ tạo ra các số có số lượng 3. Vì, 100000/32768 = 3.05 Bạn nhận được bội số của 3. Số nguyên ngẫu nhiên sẽ được nhân với 3.05 để phù hợp giữa 0 đến 100000. rand () hoạt động tốt, nếu bạn không yêu cầu số lượng lớn hơn thì rand_max. function GeraHash($qtd){
Ishtar ¶ 14 năm trước;} //Here you specify how many characters the returning string must have echo GeraHash(30); ?> Một nhận xét nhỏ về kết luận của PHPDEV-Dunnbypauls rằng rand () chỉ tạo ra các số có số lượng 3. Vì, 100000/32768 = 3.05 Bạn nhận được bội số của 3. Số nguyên ngẫu nhiên sẽ được nhân với 3.05 để phù hợp giữa 0 đến 100000. rand () hoạt động tốt, nếu bạn không yêu cầu số lượng lớn hơn thì rand_max. ¶ PHPDEV tại Dunnbypaul dot net ¶ Đây là một lưu ý thú vị về sự thấp kém của hàm rand (). Ví dụ, thử mã sau ... //~ srand(9);x Ishtar ¶ 14 năm trước= 0 ; $i < 100 ; $i++){$x = gen(); echo $x,PHP_EOL; Một nhận xét nhỏ về kết luận của PHPDEV-Dunnbypauls rằng rand () chỉ tạo ra các số có số lượng 3. Vì, 100000/32768 = 3.05 Bạn nhận được bội số của 3. Số nguyên ngẫu nhiên sẽ được nhân với 3.05 để phù hợp giữa 0 đến 100000. rand () hoạt động tốt, nếu bạn không yêu cầu số lượng lớn hơn thì rand_max. ?>PHPDEV tại Dunnbypaul dot net ¶ ¶ Đây là một lưu ý thú vị về sự thấp kém của hàm rand (). Ví dụ, thử mã sau ... Mảng (& nbsp; & nbsp; [0] => 31 & nbsp; & nbsp; [1] => 0 & nbsp; & nbsp; [2] => 0 & nbsp; & nbsp; [3] => 31 & nbsp; & nbsp; [4] ; [5] => 0 & nbsp; & nbsp; [6] => 30 & nbsp; & nbsp; [7] => 0 & nbsp; & nbsp; > 0) Trong phạm vi này chỉ có bội số của 3 đang được chọn. Cũng lưu ý rằng các giá trị được điền luôn luôn là 30 hoặc 31 (không có giá trị nào khác! Thực sự!) function s($length){ Ishtar ¶ ¶ 14 năm trước Một nhận xét nhỏ về kết luận của PHPDEV-Dunnbypauls rằng rand () chỉ tạo ra các số có số lượng 3. Vì, 100000/32768 = 3.05 Bạn nhận được bội số của 3. Số nguyên ngẫu nhiên sẽ được nhân với 3.05 để phù hợp giữa 0 đến 100000. rand () hoạt động tốt, nếu bạn không yêu cầu số lượng lớn hơn thì rand_max. PHPDEV tại Dunnbypaul dot net ¶ ¶ 14 năm trước Một nhận xét nhỏ về kết luận của PHPDEV-Dunnbypauls rằng rand () chỉ tạo ra các số có số lượng 3. Vì, 100000/32768 = 3.05 Bạn nhận được bội số của 3. Số nguyên ngẫu nhiên sẽ được nhân với 3.05 để phù hợp giữa 0 đến 100000. rand () hoạt động tốt, nếu bạn không yêu cầu số lượng lớn hơn thì rand_max. PHPDEV tại Dunnbypaul dot net ¶ Đây là một lưu ý thú vị về sự thấp kém của hàm rand (). Ví dụ, thử mã sau ... Mảng (& nbsp; & nbsp; [0] => 31 & nbsp; & nbsp; [1] => 0 & nbsp; & nbsp; [2] => 0 & nbsp; & nbsp; [3] => 31 & nbsp; & nbsp; [4] ; [5] => 0 & nbsp; & nbsp; [6] => 30 & nbsp; & nbsp; [7] => 0 & nbsp; & nbsp; > 0) Trong phạm vi này chỉ có bội số của 3 đang được chọn. Cũng lưu ý rằng các giá trị được điền luôn luôn là 30 hoặc 31 (không có giá trị nào khác! Thực sự!) Bây giờ hãy thay thế rand () bằng mt_rand () và xem sự khác biệt ... Hugo Scott hrmscott at hotmail dot com ¶ 13 years ago 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: opbarnes ¶ 13 years ago Generate a random 5 character alpha string: print preg_replace('/([ ])/e', 'chr(rand(97,122))',
' '); bozo_z_clown at yahoo dot com ¶ 15 years ago Note that the automatic seeding seems to be done with the current number of seconds which means you can get the same results for several runs on a fast server. Either call srand() yourself with a more frequently changing seed or use mt_rand() which doesn't appear to suffer from the problem. pasafama at gmail dot com ¶ 5 years ago From PHP 7.1 rand() is documented as an alias of mt_rand(). Actually, if they are called with two arguments where the second is smaller than the first, their output differs. For example var_dump(rand(2,1), mt_rand(2,1)); may return Warning: mt_rand(): max(1) is smaller than min(2) where rand() will return a random value between $max and $min Anonymous ¶ 14 years ago quick way to generate randomish numbers and simple strings. not the most perfect for sure, but ok for plenty of situations... $random_number = intval( "0" . rand(1,9) . rand(0,9) . rand(0,9) . rand(0,9) . rand(0,9) ); // random(ish) 5 digit int$random_string = chr(rand(65,90)) . chr(rand(65,90)) . chr(rand(65,90)) . chr(rand(65,90)) . chr(rand(65,90)); // random(ish) 5 character string?>hope someone finds it useful for somthing. regards, John Galt ¶ 12 years ago Another way to create an array of random numbers where there are no identical numbers. ($n = number of random numbers to return in the array function uniqueRand($n, $min = 0, $max = null) thibault dot debatty at gmail dot com ¶ 10 years ago In Suhosin version 0.9.26 (released 2008.08.22) and above: davidsteinsland [at] gmail [dot] com ¶ 14 years ago emad_ramahi at hotmail dot com: The way I see it, you have to workable solutions: Using PHP: //$Table holds the name of the table we're getting the random row from ludicruz at yahoo dot com ¶ 15 years ago frank, nick at nerdynick dot com, and kniht function rand_permute($size, $min, $max) Zak ¶ 10 years ago I couldn't find a suitable random alpha-numeric generator function so I rolled my own. It gives a random number in base 36 (0-9, a-z) to a given length. function randomAlphaNum($length){ $rangeMin = pow(36, $length-1); //smallest number to give length digits in base 36 mparsa1372 at gmail dot com ¶ 1 year ago Change Background Color With Rand() And Style :
$r = rand(0,255);$g = rand(0,255); $b = rand(0,255); echo '$r, $g, $b)" . '">';?> matthias dot isler at gmail dot com ¶ 10 years ago I had to create a function that generates a random binominal distributed integer. Take a look at the following Wiki article: http://en.wikipedia.org/wiki/Binomial_distribution Here is my solution: function bin_rand($min = null, $max = null) Anonymous ¶ 12 years ago Generate a random 5 character A-Z0-9 string for ($i=0; $i<6; $i++) { moonwalker509 at gmail dot com ¶ 5 years ago I wrote this many years back and just found it. thought I share. what it does is generate either random numbers 0-9, Alphabets A-Z or Alphanumeric which is a combination of numbers and alphabets. I turned it into a class before sharing. do with it what you please. Solved some of my problems. class Random{ for ( $i = 0; $i < $length; $i++) {$id .= $chars[mt_rand(0,$clen)]; } return ($id); } public static function Alphabets($length){ $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; $clen = strlen( $chars )-1; $id = ''; for ( $i = 0; $i < $length; $i++) {$id .= $chars[mt_rand(0,$clen)]; } return ($id); } public static function AlphaNumeric($length){ $chars = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; $clen = strlen( $chars )-1; $id = ''; for ( $i = 0; $i < $length; $i++) {$id .= $chars[mt_rand(0,$clen)]; } return ($id); } } echo Random::Numeric(6); # eg result: "567268"echo Random::Alphabets(9); # eg result: IAGRmZyJS echo Random::AlphaNumeric(10); #eg result: Gzt6syUS8M?> david [at] ddrewdesign [dot] com ¶ 13 years ago To Jano and Peta: Thanks for the code. In real world usage, I only had one problem with it: It will never return the first result of the array (or it will return nothing if there's only one item in the array). To remedy this, I simply subtracted 1 from $rand = rand(1,$max); $rand = rand(1,$max)-1; Alex Khimch alex at khim dot removeit dot ich dot org ¶ 11 years ago Random is NOT actually random. It is easily illustrated by multiplying rand(1,500) by rand(1,500) and showing the output on the image: header("Content-type: image/png"); for( $i=0;$i=0;$i<500;$i++) {for($j=0;$j<500;$j++) { imagesetpixel($img, rand(1,500), rand(1,500), $ink1); } }imagepng($img); imagedestroy($img);?> I expected to get pixel noise, but instead one can see plain diagonal lines. admin at djs-music dot com ¶ 13 years ago A nice function to generate a random string, using any character: function generateRandStr($length){ Sample output: $%29zon(4f simon at labs dot coop ¶ 8 years ago Something we discovered in Sydney running BBS Systems before the net advent was here, if we didn't seed of another BBS we would going in circles in our System Physicality Abstraction Layers.. The important thing is to seed from a remote system and easy way at the Centroidal Plexus of the web (Chronolabs Cooperative) we offer a seed feed and the following code will randomise you out of the number cycle: See in PHP both the letters and numbers are seedable as letters are treated as numbers as well. You can always use individual tokens by extracting the Element with DOM.. But below is equally effective! srand (file_get_contents('http://seed.feeds.labs.coop')); ?>szeryf.wordpress.com ¶ 11 years ago Much easier way to generate random string of numbers and letters: $n
= rand(10e16, 10e20); Greg R. ¶ 13 years ago I thought this function (random color) might be of use to someone [to create and return a random hex for HTML colors]: function get_random_color() liveonaware [at] gmail [dot] com ¶ 12 years ago //To Pull 7 Unique Random Values Out Of AlphaNumeric //removed number 0,
capital o, number 1 and small L "A","B","C","D","E","F","G","H","J","K","L","M", "N","P","Q","R","S","T","U","V","W","X","Y","Z", "1","2","3","4","5","6","7","8","9");//make an "empty container" or array for our keys $keys = array();//first count of $keys is empty so "1", remaining count is 1-6 = total 7 times while(count($keys) < 7) { //"0" because we use this to FIND ARRAY KEYS which has a 0 value //"-1" because were only concerned of number of keys which is 32 not 33 //count($characters) = 33 $x = mt_rand(0, count($characters)-1); if(!in_array($x, $keys)) { $keys[] = $x; } } foreach( $keys as $key){$random_chars .= $characters[$key]; } echo $random_chars; ?> smaaps at kaldamar dot de ¶ 17 years ago 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. function gauss($algorithm = "polar") { switch( $algorithm) { //polar-methode by marsagliacase "polar": $v = 2; while ($v > 1) { $u1 = rand(0, $randmax) / $randmax; $u2 = rand(0, $randmax) / $randmax; $v = (2 * $u1 - 1) * (2 * $u1 - 1) + (2 * $u2 - 1) * (2 * $u2 - 1); } & nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;trở về ( 2* $u1 - 1) * (( -2 * log($v) / $v) ^ 0.5); // box-muller-methodcase "boxmuller": do { $u1 = rand(0, $randmax) / $randmax; $u2 = rand(0, $randmax) / $randmax; $x = sqrt(-2 * log($u1)) * cos(2 * pi() * $u2); } while (strval($x) == "1.#INF" or strval($x) == "-1.#INF"); // the check has to be done cause sometimes (1:10000) // values such as "1.#INF" occur and i dont know why return $x; // twelve random numbers case "zwoelfer": $sum = 0; for ($i = 0; $i < 12; $i++) { $sum += rand(0, $randmax) / $randmax; } return $sum; } } ?> Ẩn danh ¶ ¶ 1 năm trước Lưu ý rằng thay đổi thuật toán trong phiên bản 7.1.0 đã phá vỡ độ lặp lại của một chuỗi ngẫu nhiên được khởi tạo với một giá trị nhất định.Ví dụ: nếu bạn có một chương trình như: srand($argv[1]); |