Tôi có điều này:PHP rand() loại trừ các số nhất định
<?php $n = rand(1,1600); echo $n ?>
Tôi muốn loại trừ từ số ngẫu nhiên giả sử 234, 1578, 763, 1274 và số khác. Tôi sẽ làm như thế nào?
Tôi có điều này:PHP rand() loại trừ các số nhất định
<?php $n = rand(1,1600); echo $n ?>
Tôi muốn loại trừ từ số ngẫu nhiên giả sử 234, 1578, 763, 1274 và số khác. Tôi sẽ làm như thế nào?
<?php
while(in_array(($n = rand(1,1600)), array(234, 1578 ,763 , 1274)));
Không sử dụng rand, sử dụng mt_rand hoặc random_int tốt hơn nếu bạn đến từ PHP7. –
Hãy thử như
do {
$n = rand(1,1600);
} while(in_array($n, array(234, 1578 ,763 , 1274));
echo $n;
nó sẽ kiểm tra xem các số được tạo ra trong mảng hay không, nếu họ có mặt sau đó một lần nữa nó sẽ goto vòng lặp do-while – Gautam3164
nvm, tôi chỉ nhận ra những gì tôi nói là sai – Menztrual
Kiểm tra này nếu số là một trong đó bạn không muốn, nếu nó là có được một số ngẫu nhiên mới.
function getRandomNumber() {
do {
$n = mt_rand(1,1600);
} while(in_array($n, array(234,1578, 763, 1274)));
return $n;
}
Tại sao không đệ quy? –
@ClaudioLudovicoPanetta - tại sao làm cho nó đệ quy, nếu một vòng lặp trong khi hoạt động hoàn hảo? – andrewsi
@andrewsi vì đệ quy luôn mát :-P –
Bạn có thể tạo một mảng có số hợp lệ.
Sau đó, tạo số ngẫu nhiên của bạn sẽ trả về chỉ mục vào mảng đó.
Nếu bạn không có quá nhiều số điện thoại để loại trừ, đó là dễ dàng hơn và nhanh hơn để chỉ thử lại nếu bạn tìm thấy một số không mong muốn:
$n = 0;
while (in_array($n, array(0, 234, 1578 ,763 , 1274))) {
$n = rand(1,1600);
}
echo $n;
Hoặc tránh đưa ra các vòng lặp với ngẫu nhiên (có thể là vô hạn) thời gian chạy:
/**
* Returns a random integer between $min and $max (inclusive) and
* excludes integers in $exarr, returns false if no such number
* exists.
*
* $exarr is assumed to be sorted in increasing order and each
* element should be unique.
*/
function random_exclude($min, $max, $exarr = array()) {
if ($max - count($exarr) < $min) {
return false;
}
// $pos is the position that the random number will take
// of all allowed positions
$pos = rand(0, $max - $min - count($exarr));
// $num being the random number
$num = $min;
// while $pos > 0, step to the next position
// and decrease if the next position is available
for ($i = 0; $i < count($exarr); $i += 1) {
// if $num is on an excluded position, skip it
if ($num == $exarr[$i]) {
$num += 1;
continue;
}
$dif = $exarr[$i] - $num;
// if the position is after the next excluded number,
// go to the next excluded number
if ($pos >= $dif) {
$num += $dif;
// -1 because we're now at an excluded position
$pos -= $dif - 1;
} else {
// otherwise, return the free position
return $num + $pos;
}
}
// return the number plus the open positions we still had to go
return $num + $pos;
}
Chức năng này chọn vị trí ngẫu nhiên và đi vào mảng loại trừ để tìm vị trí miễn phí. Đó là thời gian chạy phụ thuộc vào số lượng các số để loại trừ. Nếu bạn muốn loại trừ các phạm vi nhất định, bạn có thể muốn điều chỉnh thuật toán để tính đến điều này.
Một giải pháp cho điều này có thể thực hiện như sau:
function random_number($min, $max, $exclude)
{
$number = rand($min, $max);
if(in_array($number, $exlude))
{
random_number($min, $max, $exlude);
} else {
return $number;
}
}
$number = random_number(1,10, [2,5,6]);
Luôn luôn sử dụng các thuật toán mã hóa mạnh mẽ để tạo ra số ngẫu nhiên:
/**
* @param int $from From number
* @param int $to To number
* @param array $excluded Additionally exclude numbers
* @return int
*/
function randomNumber($from, $to, array $excluded = [])
{
$func = function_exists('random_int') ? 'random_int' : 'mt_rand';
do {
$number = $func($from, $to);
} while (in_array($number, $excluded, true));
return $number;
}
var_dump(randomNumber(1, 100));
var_dump(randomNumber(1, 10, [5, 6, 7, 8]));
var_dump(randomNumber(1, 100, range(10, 90)));
Không sử dụng rand! Sử dụng [mt_rand] (http://php.net/mt_rand) hoặc [random_int] tốt hơn (http://php.net/random_int) nếu bạn đến từ PHP7. –