Hướng dẫn range az php - phạm vi az hp

[Php 4, Php 5, Php 7, Php 8]

Nội dung chính

  • Sự mô tả
  • Danh sách cài đặt
  • Trả về giá trị
  • Lịch sử
  • Đượ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].
  • Danh sách cài đặt

Trả về giá trịGénère une valeur aléatoire

Sự mô tả

Danh sách cài đặt[]: int

Trả về giá trị[int $min, int $max]: int

Lịch sử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].

Đượ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 - Tạo giá trị ngẫu nhiên — Génère une valeur aléatoirerandom_int[], random_bytes[], ou openssl_random_pseudo_bytes[].

Rand []: int[]: int: 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.

Rand [int $ min, int $ max]: int[int $min, int $max]: int: À 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[]

Danh sách cài đặt

Trả về giá trị

Lịch sử

Đượ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 - Tạo giá trị ngẫu nhiên — Génère une valeur aléatoiremt_getrandmax[]]

Trả về giá trị

Lịch sửmt_getrandmax[], inclusif].

Lịch sử

Đượ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].Sự mô tả
7.2.0 RAND - Tạo giá trị ngẫu nhiên — Génère une valeur aléatoire a reçu une correction de bogue pour un bug de polarisation modulo. Cela signifie que les séquences générées dans certain cas spécifiques peuvent différer de php 7.1 sur les machines 64-bit.
7.1.0 Rand []: int[]: int a été fait un alias de mt_rand[].

Đượ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 - Tạo giá trị ngẫu nhiên — Génère une valeur aléatoirerand[]

Rand []: int[]: int
echo rand[] . "\n";
echo 
rand[] . "\n";

Rand [int $ min, int $ max]: int[int $min, int $max]: int

rand[5, 15];?>[5, 15];?>[515];
?>

Đượ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].

Danh sách cài đặt

Trả về giá trị

Lịch sửgetrandmax[]. i.e. [max - min]

If you don't want the same character to appear beside itself, use this:

Kết quả của ví dụ trên tương tự như:for [$i = 0, $z = strlen[$a = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890']-1, $s = $a{rand[0,$z]}, $i = 1; $i != 32; $x = rand[0,$z], $s .= $a{$x}, $s = [$s{$i} == $s{$i-1} ? substr[$s,0,-1] : $s], $i=strlen[$s]]; ?> For those of you who want both as a function, use this:
for [$i = 0, $z = strlen[$a = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890']-1, $s = $a{rand[0,$z]}, $i = 1; $i != 32; $x = rand[0,$z], $s .= $a{$x}, $s = [$s{$i} == $s{$i-1} ? substr[$s,0,-1] : $s], $i=strlen[$s]];
?>

For those of you who want both as a function, use this:

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 [].function rand_chars[$c, $l, $u = FALSE] { if [!$u] for [$s = '', $i = 0, $z = strlen[$c]-1; $i < $l; $x = rand[0,$z], $s .= $c{$x}, $i++]; else for [$i = 0, $z = strlen[$c]-1, $s = $c{rand[0,$z]}, $i = 1; $i != $l; $x = rand[0,$z], $s .= $c{$x}, $s = [$s{$i} == $s{$i-1} ? substr[$s,0,-1] : $s], $i=strlen[$s]]; return $s; } ?> 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.
function rand_chars[$c, $l, $u = FALSE] {
if [!
$u] for [$s = '', $i = 0, $z = strlen[$c]-1; $i < $l; $x = rand[0,$z], $s .= $c{$x}, $i++];
else for [
$i = 0, $z = strlen[$c]-1, $s = $c{rand[0,$z]}, $i = 1; $i != $l; $x = rand[0,$z], $s .= $c{$x}, $s = [$s{$i} == $s{$i-1} ? substr[$s,0,-1] : $s], $i=strlen[$s]];
return
$s;
}
?>

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.

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ế.rand_chars["ABCEDFG", 10] == GABGFFGCDA rand_chars["ABCEDFG", 10, TRUE] == CBGFAEDFEC
rand_chars["ABCEDFG", 10] == GABGFFGCDA
rand_chars["ABCEDFG", 10, TRUE] == CBGFAEDFEC

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]rand[]&1;// instead ofrand[0,1];// for generating 0 or 1,rand[]&3;// instead ofrand[0,3];// for generating 0, 1, 2, or 3,rand[]&7;// instead ofrand[0,7]// for generating 0, 1, 2, 3, 4, 5, 6, or 7,?>and so on. All you're doing there is generating a default random number [so PHP doesn't have to parse any arguments] and chopping off the piece that's useful to you [using a bitwise operation which is faster than even basic math].
rand
[]&1;
// instead of
rand[0,1];
// for generating 0 or 1,rand[]&3;
// instead of
rand[0,3];
// for generating 0, 1, 2, or 3,rand[]&7;
// instead of
rand[0,7]
// for generating 0, 1, 2, 3, 4, 5, 6, or 7,
?>

and so on. All you're doing there is generating a default random number [so PHP doesn't have to parse any arguments] and chopping off the piece that's useful to you [using a bitwise operation which is faster than even basic math].

Tối đa ¶

Ví dụ

Ví dụ #1 Ví dụ với Rand []

// estimate the number of rows in a table$lekerdezes = mysql_query["select count[*] as rows from table"];while [$row = mysql_fetch_assoc[$lekerdezes]]{    $max = $row["rows"];}?>the count[*] is much faster for the database than grabbing the hole dataset from the table.
$lekerdezes = mysql_query["select count[*] as rows from table"];
while [
$row = mysql_fetch_assoc[$lekerdezes]]
{
   
$max = $row["rows"];
}
?>
the count[*] is much faster for the database than grabbing the hole dataset from the table.

tiếng vang ¶

Kết quả của ví dụ trên tương tự như:

Ghi chúit's not scientifically approved, but worked for me.
it's not scientifically approved, but worked for me.

Cảnh báo/* * @param float  $mean, desired average * @param number $sd, number of items in array * @param number $min, minimum desired random number * @param number $max, maximum desired random number * @return array */ function array_distribute[$mean,$sd,$min,$max]{     $result = array[];     $total_mean = intval[$mean*$sd];     while[$sd>1]{         $allowed_max = $total_mean - $sd - $min;         $allowed_min = intval[$total_mean/$sd];         $random = mt_rand[max[$min,$allowed_min],min[$max,$allowed_max]];         $result[]=$random;         $sd--;         $total_mean-=$random;     }     $result[] = $total_mean;     return $result; } ?>
/*
* @param float  $mean, desired average
* @param number $sd, number of items in array
* @param number $min, minimum desired random number
* @param number $max, maximum desired random number
* @return array
*/
function array_distribute[$mean,$sd,$min,$max]{
   
$result = array[];
   
$total_mean = intval[$mean*$sd];
    while[
$sd>1]{
       
$allowed_max = $total_mean - $sd - $min;
       
$allowed_min = intval[$total_mean/$sd];
       
$random = mt_rand[max[$min,$allowed_min],min[$max,$allowed_max]];
       
$result[]=$random;
       
$sd--;
       
$total_mean-=$random;
    }
   
$result[] = $total_mean;
    return
$result;
}
?>

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ể    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
    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

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";$code = md5[$rand . time[]];echo "$code";
$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 ý: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:
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:

// estimate the number of rows in a table$lekerdezes = mysql_query["select * from table"];$max = mysql_num_rows[$lekerdezes];// pick one$rand = rand[1,$max];$lekerdezes2 = mysql_query["select * from table limit $rand, 1"];// that's all folks :]?>
$lekerdezes = mysql_query["select * from table"];
$max = mysql_num_rows[$lekerdezes];// pick one
$rand = rand[1,$max];$lekerdezes2 = mysql_query["select * from table limit $rand, 1"];// that's all folks :]?>

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.$n = rand[0,100000]; // with MAX_RAND=32768?>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.
$n = rand[0,100000]; // with MAX_RAND=32768
?>
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.

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];for [$i=0;$i= array[0,0,0,0,0,0,0,0,0,0,0];
for [
$i=0;$i 31    [1] => 0    [2] => 0    [3] => 31    [4] => 0    [5] => 0    [6] => 30    [7] => 0    [8] => 0    [9] => 31    [10] => 0]
[
    [0] => 31
    [1] => 0
    [2] => 0
    [3] => 31
    [4] => 0
    [5] => 0
    [6] => 30
    [7] => 0
    [8] => 0
    [9] => 31
    [10] => 0
]

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.$n = rand[0,100000]; // with MAX_RAND=32768?>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.

PHPDEV tại Dunnbypaul dot net ¶ ¶
[
    [0] => 8
    [1] => 8
    [2] => 14
    [3] => 16
    [4] => 9
    [5] => 11
    [6] => 8
    [7] => 9
    [8] => 7
    [9] => 7
    [10] => 9
]

Đâ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];for [$i=0;$i

Ishtar ¶[    [0] => 31    [1] => 0    [2] => 0    [3] => 31    [4] => 0    [5] => 0    [6] => 30    [7] => 0    [8] => 0    [9] => 31    [10] => 0]

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 ¶[    [0] => 8    [1] => 8    [2] => 14    [3] => 16    [4] => 9    [5] => 11    [6] => 8    [7] => 9    [8] => 7    [9] => 7    [10] => 9]
substr[str_shuffle[str_repeat['ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',5]],0,5];
?>
The strings can be repeated to have the possibility that a character appears multiple times.

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]substr[str_shuffle[str_repeat['ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',5]],0,5];?>The strings can be repeated to have the possibility that a character appears multiple times.
function rand_str[$length = 32, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890']
{
   
// Length of character list
   
$chars_length = [strlen[$chars] - 1];// Start our string
   
$string = $chars{rand[0, $chars_length]};// Generate random string
   
for [$i = 1; $i < $length; $i = strlen[$string]]
    {
       
// Grab a random character from our list
       
$r = $chars{rand[0, $chars_length]};// Make sure the same two characters don't appear next to each other
       
if [$r != $string{$i - 1}] $string .=  $r;
    }
// Return the string
   
return $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.$n = rand[0,100000]; // with MAX_RAND=32768?>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.

PHPDEV tại Dunnbypaul dot net ¶ ¶GeraHash[$qtd]{
//Under the string $Caracteres you write all the characters you want to be used to randomly generate the code.
$Caracteres = 'ABCDEFGHIJKLMOPQRSTUVXWYZ0123456789';
$QuantidadeCaracteres = strlen[$Caracteres];
$QuantidadeCaracteres--; $Hash=NULL;
    for[
$x=1;$x

$r = array[0,0,0,0,0,0,0,0,0,0,0];for [$i=0;$i

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];xfunction gen[]{$len = 10 ; $x = '';    for [$i = 0 ; $i < $len ; $i ++]    {        $x .= intval[rand[0,9]];    }return $x;}
function gen[]{
$len = 10 ;
$x = '';
    for [
$i = 0 ; $i < $len ; $i ++]
    {
       
$x .= intval[rand[0,9]];
    }
return
$x;
}

Ishtar ¶

14 năm trước= 0 ; $i < 100 ; $i++]{$x = gen[];echo $x,PHP_EOL;?>= 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 ...

//~ srand[9];xfunction gen[]{$len = 10 ; $x = '';    for [$i = 0 ; $i < $len ; $i ++]    {        $x .= intval[rand[0,9]];    }return $x;}

Ishtar ¶

14 năm trước= 0 ; $i < 100 ; $i++]{$x = gen[];echo $x,PHP_EOL;?>s[$length]{
  for[
$i=0;[$i

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 ¶ ¶

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]
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:
$s=1;    // Start value
$c=50;    // Count / End value
$test=array_fill[$s, $c, 0];
$ts=microtime[true];
for[
$i=0; $i$v] echo "$k :\t$v
\n"
;
?>

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.

PHPDEV tại Dunnbypaul dot net ¶ ¶

PHPDEV tại Dunnbypaul dot net ¶ ¶

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 ¶ ¶
4267 = [13050a+c] % 32768
25352 = [4267a+c] % 32768

Đâ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]
c = 12345

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 ¶ ¶

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]{  for[$i=0;[$i

Ishtar ¶ ¶
function makeRandomDateInclusive[$startDate,$endDate]{
   
$days = round[[strtotime[$endDate] - strtotime[$startDate]] / [60 * 60 * 24]];
   
$n = rand[0,$days];
    return
date["Y-m-d",strtotime["$startDate + $n days"]];   
}
?>

and the one-line version for compactness freaks:
function makeRandomDateInclusive[$startDate,$endDate]{   
    return
date["Y-m-d",strtotime["$startDate + ".rand[0,round[[strtotime[$endDate] - strtotime[$startDate]] / [60 * 60 * 24]]]." days"]];
}
?>

it is called like this
echo makeRandomDateInclusive['2009-04-01','2009-04-03'];
?>
Hope this is of some use to someone

Opbarnes ¶

13 năm trước

Tạo chuỗi alpha 5 ký tự ngẫu nhiên:

in preg_replace ['/[[]]/e', 'chr [rand [97,122]]', '& nbsp; & nbsp; & nbsp;'];?>?preg_replace['/[[ ]]/e', 'chr[rand[97,122]]', '     '];
?>

bozo_z_clown tại yahoo dot com ¶

15 năm trước

Lưu ý rằng việc gieo hạt tự động dường như được thực hiện với số giây hiện tại có nghĩa là bạn có thể nhận được kết quả tương tự cho một số lần chạy trên máy chủ nhanh. & NBSP; Hoặc tự gọi srand [] với một hạt giống thay đổi thường xuyên hơn hoặc sử dụng mt_rand [] mà dường như không gặp phải vấn đề.

Pasafama tại gmail dot com ¶

5 năm trước

Từ Php 7.1 rand [] được ghi nhận là bí danh của mt_rand [].

Trên thực tế, nếu chúng được gọi với hai đối số trong đó thứ hai nhỏ hơn so với thứ nhất, đầu ra của chúng khác nhau.

Ví dụ

var_dump [rand [2,1], mt_rand [2,1]];

có thể trở lại

CẢNH BÁO: MT_RAND []: Max [1] nhỏ hơn Min [2] Int [2] Bool [Sai]
int[2]
bool[false]

trong đó rand [] sẽ trả về một giá trị ngẫu nhiên giữa $ max và $ min

Ẩn danh ¶

14 năm trước

Cách nhanh chóng để tạo các số ngẫu nhiên và các chuỗi đơn giản. Không làm rối xung quanh với các chức năng, vì vậy bạn có thể chỉ cần bật dòng vào giữa mã hiện tại của bạn.
no messing around with functions, so you can just pop the line into the middle of your existing code.

Không phải là hoàn hảo nhất để chắc chắn, nhưng ok cho nhiều tình huống ...

$ ngẫu nhiên_number

= intval ["0". Rand [1,9]. Rand [0,9]. Rand [0,9]. Rand [0,9]. Rand [0,9]]; // ngẫu nhiên [ish] 5 chữ số int $ ngẫu nhiên_string = chr [rand [65,90]]. CHR [RAND [65,90]]. CHR [RAND [65,90]]. CHR [RAND [65,90]]. CHR [RAND [65,90]]; // Chuỗi nhân vật ngẫu nhiên [ish] 5?> Hy vọng ai đó thấy nó hữu ích cho thứ gì đó.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.

Trân trọng, Deeeeeeen alxndr0u
deeeeeen alxndr0u

John Galt ¶

12 năm trước

Một cách khác để tạo một mảng các số ngẫu nhiên trong đó không có số giống hệt nhau.

[$ n = số lượng số ngẫu nhiên để trả về trong mảng $ min = số tối thiểu $ max = số tối đa]
$min = minimum number
$max = maximum number]

hàm Uniquerand [$ n, $ min = 0, $ max = null] {& nbsp; if [$ max === null] & nbsp; & nbsp; $ max = getRandMax []; & nbsp; $ mảng = phạm vi [$ min, $ max]; & nbsp; $ return = mảng []; & nbsp; $ keys = mảng_rand [$ mảng, $ n]; & nbsp; foreach [$ khóa là $ key] & nbsp; & nbsp; $ return [] = $ mảng [$ key]; & nbsp; trả lại $ return; }?>uniqueRand[$n, $min = 0, $max = null]
{
  if[
$max === null]
  
$max = getrandmax[];
 
$array = range[$min, $max];
 
$return = array[];
 
$keys = array_rand[$array, $n];
  foreach[
$keys as $key]
  
$return[] = $array[$key];
  return
$return;
}
?>

Thibault Dot Debatty tại Gmail Dot Com ¶

10 năm trước

Trong Suhosin phiên bản 0.9.26 [phát hành 2008,08,22] trở lên:- rand [] và srand [] được sửa đổi một cách trong suốt để sử dụng thuật toán twister mersenne với trạng thái riêng biệt [] và mt_rand [] có hạt giống bên trong tốt hơn [ ] và mt_srand [] bị bỏ qua [có thể được cấu hình]
- rand[] and srand[] are transparently modified to use the Mersenne Twister algorithm with separate state
- rand[] and mt_rand[] have better internal seeding
- srand[] and mt_srand[] are ignored [can be configured]

Davidsteinsland [at] gmail [dot] com ¶

14 năm trước

Cách nhanh chóng để tạo các số ngẫu nhiên và các chuỗi đơn giản. Không làm rối xung quanh với các chức năng, vì vậy bạn có thể chỉ cần bật dòng vào giữa mã hiện tại của bạn.
I've actually noticed that with a large dataset [100k rows], the query dramatically slows down the server and performance is way too bad.

Không phải là hoàn hảo nhất để chắc chắn, nhưng ok cho nhiều tình huống ...

$ ngẫu nhiên_number

= intval ["0". Rand [1,9]. Rand [0,9]. Rand [0,9]. Rand [0,9]. Rand [0,9]]; // ngẫu nhiên [ish] 5 chữ số int $ ngẫu nhiên_string = chr [rand [65,90]]. CHR [RAND [65,90]]. CHR [RAND [65,90]]. CHR [RAND [65,90]]. CHR [RAND [65,90]]; // Chuỗi nhân vật ngẫu nhiên [ish] 5?> Hy vọng ai đó thấy nó hữu ích cho thứ gì đó.
//$Rows specifies how many rows we need to fetch
function mysql_rand [$Query, $Rows = 1] {
   
//getting the table name from $query
    //what you can do, is replace the $query argument with $table,
    //this way to dont have to search for the table's name
   
$SQL = sprintf ['SELECT COUNT[*] FROM %s', substr [$query, stripos [$query, 'from']+5, strpos [$query, ' ']]];$Max = mysql_result [mysql_query [$SQL], 0];
   
$Random = rand [0, $Max];//If the random number is 99, and the database only has 100 rows
    //We'll subtract the random number, so we don't exceed 100, thus preventing a MySQL error message
   
return $Query . sprintf ['LIMIT %d, %d', [$Max < $Rows] ? 0 : [[$Random > $Max - $Rows] ? $Max - $Rows : $Random], $Rows];
}
//Instead of using MySQL's RAND[], we use LIMIT to fetch rows
//E.g. LIMIT 5,9 fetches from row 5, and the subsequent 9
$SQL = mysql_rand ['SELECT row FROM table']; //SELECT row FROM table LIMIT x, y
?>

or SQL:
SELECT * FROM Table T JOIN [SELECT FLOOR[MAX[ID]*RAND[]] AS ID FROM Table] AS x ON T.ID >= x.ID LIMIT 1;

Trân trọng, Deeeeeeen alxndr0u

15 năm trước

Lưu ý rằng việc gieo hạt tự động dường như được thực hiện với số giây hiện tại có nghĩa là bạn có thể nhận được kết quả tương tự cho một số lần chạy trên máy chủ nhanh. & NBSP; Hoặc tự gọi srand [] với một hạt giống thay đổi thường xuyên hơn hoặc sử dụng mt_rand [] mà dường như không gặp phải vấn đề.
this is now O[n] instead of O[n^2] ish...

Pasafama tại gmail dot com ¶rand_permute[$size, $min, $max]
{
   
$retval = array[];
   
//initialize an array of integers from $min to $max
   
for[$i = $min;$i ';?>rand[0,255];
       
$g = rand[0,255];
       
$b = rand[0,255];
         echo 
' $r, $g, $b]" . '">';?>

Matthias Dot Isler tại Gmail Dot Com ¶

10 năm trước

Tôi đã phải tạo một hàm tạo ra một số nguyên phân phối nhị thức ngẫu nhiên. Hãy xem bài viết Wiki sau:

//en.wikipedia.org/wiki/binomial_distribution

Đây là giải pháp của tôi:

function bin_rand[$min = null, $max = null]
{
   
$min = [$min] ? [int] $min : 0;
   
$max = [$max] ? [int] $max : PHP_INT_MAX;$range = range[$min, $max];
   
$average = array_sum[$range] / count[$range];$dist = array[];
    for [
$x = $min; $x $quantity] {
        for [
$x = 0; $x < $quantity; $x++] {
           
$map[] = $int;
        }
    }
shuffle[$map];
    return
current[$map];
}
?>

Ẩn danh ¶

12 năm trước

Tạo 5 ký tự ngẫu nhiên A-Z0-9 & NBSP; sợi dây

for [$ i = 0; $ i # php -r 'for [$ i = 0; $ i$i=0; $i

# php -r 'for [$i=0; $i

Bài Viết Liên Quan

Chủ Đề