Fill two arrays with random numbers so that the numbers in these two arrays do not repeat. Filling should occur using rand.

define ("B" , 4); define ("C" , 4); $D = []; $E = []; for ($i = 0; $i < B; $i++) { for ($j = 0; $j < C; $j++){ $D[$i] = rand(0 , 8); $E[$j] = rand(0 , 8); } } foreach ($D as $V){ foreach ($E as $B){ if (array($V) == array($B)){ } } } 

Closed due to the fact that off-topic participants 0xdb , aleksandr barakin , Kromster , kot-da-vinci , user300000 25 Oct '18 at 20:59 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - 0xdb, aleksandr barakin, Kromster, Let's say Pie
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Too generalized! Attach your example that does not work? - Pavel Igorevich
  • So you only need to fill in with unique values? Or fill with rand() and check what is not repeated? - Yuriy Prokopets
  • Fill in with rand () so that there are no duplicate numbers in the first and second arrays - Vasily Pupkin

2 answers 2

 $arr1 = []; $arr2 = []; $temp = range(1, 10); shuffle($temp); foreach ($temp as $i => $int) { $i & 1 ? $arr1[] = $int : $arr2[] = $int; } print_r($arr1); print_r($arr2); 

UPD :

Filling must occur using rand

 $temp = []; do { $rand = rand(0, 10); $temp[$rand] = $rand; } while (count($temp) < 10); list($arr1, $arr2) = array_chunk($temp, sizeof($temp) / 2); print_r($arr1); print_r($arr2); 
  • Filling should occur using rand - Vasily Pupkin

Randomly mix an array of unique numbers, and then chop it into two parts.

 $a = range(1, 10); usort($a, function($a, $b){return rand(-1, 1);}); [$foo, $bar] = array_chunk($a, 5); echo implode(", ", $foo) . "\n"; // 5, 1, 2, 6, 7 echo implode(", ", $bar) . "\n"; // 3, 4, 8, 9, 10 

Caution! Such a hack with sorting through rand gives a not very pleasant "coincidence" to the eye.