Good day! I'm just starting to comprehend php. Confused now with this moment: created a number generator using the mt_rand function. The problem is that I don’t know how to check the uniqueness of the generated numbers, weed out the duplicate and generate the new missing quantity. Help please solve the problem. Here is the code:

 <?php $even = $_POST['even']; $odd = $_POST['odd']; $number = $_POST['pir']; $start2 = $_POST['start2']; $end2 = $_POST['end2']; function clean($value = "") { $value = trim($value); //для удаления пробелов из начала и конца строки $value = stripslashes($value); //для удаления экранированных символов ("Ваc зовут O\'reilly?" => "Вас зовут O'reilly?") $value = strip_tags($value); //для удаления HTML и PHP тегов $value = htmlspecialchars($value); //преобразует специальные символы в HTML-сущности ('&' преобразуется в '&' и т.д.) return $value; } $number = clean($number); $start2 = clean($start2); $end2 = clean($end2); for ($i=1; $i<=$number; $i++){ if ($number>300) { echo '<span style="display:inline-block; margin-left:20px; margin-bottom:20px;">Максимальное количество сгенерированных чисел не может больше 100</span>'; exit(); } elseif ($end2 <= $start2) { echo '<span style="display:inline-block; margin-left:20px; margin-bottom:20px;">Введите второе число диапазона большее, чем первое.</span>'; exit(); } $num1 = mt_rand($start2, $end2); if (isset($even) or isset($odd)) { if (isset($even)) { if ($num1 % 2 == 0) { echo '<span style="display:inline-block; margin-left:20px; margin-bottom:20px;">'.$num1.'</span>'; } else { $numeven = $num1 +1; echo '<span style="display:inline-block; margin-left:20px; margin-bottom:20px;">'.$numeven.'</span>'; } } elseif (isset($odd)) { if ($num1 % 2 !== 0) { echo '<span style="display:inline-block; margin-left:20px; margin-bottom:20px;">'.$num1.'</span>'; } else { $numodd = $num1 - 1; echo '<span style="display:inline-block; margin-left:20px; margin-bottom:20px;">'.$numodd.'</span>'; } } } else { echo '<span style="display:inline-block; margin-left:20px; margin-bottom:20px;">'.$num1.'</span>'; } } ?> 
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

Option for small ranges $ min- $ max.

 $target = array_slice(shuffle(range($min, $max)),0,$num); 

Option for large ranges $ min- $ max

 $tmp = array(); while(count($tmp)<$num)$tmp[mt_rand($min,$max)]=true; $target = array_keys($tmp); 
  • Hmm, I'm pouring out here, shortening the code, and some Pro wrote it in 3 lines. Plus - Mr. Black
  • memory is so bad. at min=0 and max=2^20 ate about 60mb - Artyom
  • @ Artem, which is logical. There is no universal, good method. Under each task your tool. - rjhdby
 $arr = gen(5, 0, 4); echo var_dump($arr); 

console.log

 3 повтор; 4 повтор; 4 повтор; 2 повтор; array(5) { [0]=>int(0) [1]=>int(3) [2]=>int(2) [3]=>int(4) [4]=>int(1) } 

 function gen($num, $min, $max) { if($num > $max - $min + 1) return; $arr = array(); for($n = 0; count($arr) < $num; $n = mt_rand($min, $max)) { if(!in_array($n, $arr)) { array_push($arr, $n); } else { echo $n.' повтор; '; } } return $arr; } 

With a smaller value of num relative to max-min returns NULL gen(5, 0, 3)

  • 2
    Yeah, KISS did not even pass by here. - PinkTux
  • > gen (5, 1, 4)> if (in_array ($ n, $ arr)) array 4 cool - strangeqargo
  • @strangeqargo, corrected. With gen(10, 20, 29) everything is OK, with gen(10, 20, 28) NULL - Mr. Black
  • you have there in_array, which you pass the number to, person php.net/manual/ru/function.in-array.php - strangeqargo
  • @strangeqargo, The example is quite working. If you can get better, answer me, we will all learn from you - Mr. Black