Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This seems to satisfy your conditions: <a href="http://codepad.viper-7.com/WHkQeD" rel="nofollow">http://codepad.viper-7.com/WHkQeD</a></p> <pre><code>&lt;?php $num_arrays = 10; $num_elements = 4; $min = 1; $max = 20; $exclude_numbers = array( 1, 4, 6); // Add numbers here to exclude $answer = array(); for( $i = 0; $i &lt; $num_arrays; $i++) { $answer[$i] = array(); for( $j = 0; $j &lt; $num_elements; $j++) { do { $current = rand( $min, $max); // If the previous array exists and there are more than two common elements when we add the $current element, continue if( isset( $answer[$i-1]) &amp;&amp; count( array_intersect( $answer[$i-1], array_merge( $answer[$i], array( $current))) &gt; 2) { continue; } } while( in_array( $current, $exclude_numbers) || in_array( $current, $answer[$i])); $answer[$i][$j] = $current; } } var_dump( $answer); </code></pre> <p><strong>Edit:</strong> Here is a complete solution that satisfies all of your criteria.</p> <p><a href="http://codepad.viper-7.com/aN1I50" rel="nofollow">Demo</a></p> <pre><code>&lt;?php $num_arrays = 10; $num_elements = 4; $min = 1; $max = 20; $exclude_numbers = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13); $answer = array(); for( $i = 0; $i &lt; $num_arrays; $i++) { $answer[$i] = array(); for( $j = 0; $j &lt; $num_elements; $j++) { do { // Get a random element $current = rand( $min, $max); $new_array = array_merge( $answer[$i], array( $current)); // If the previous array has more than two common elements (because of the added $current), get a new $current if( isset( $answer[$i-1]) &amp;&amp; count( array_intersect( $answer[$i-1], $new_array)) &gt; 2) { $answer[$i] = array_diff( $new_array, $answer[$i-1]); $j = count( $answer[$i]) - 1; continue; } } while( in_array( $current, $exclude_numbers) || in_array( $current, $answer[$i])); $answer[$i][$j] = $current; // If the array is complete, we need to check for unique arrays if( count( $answer[$i]) == $num_elements) { $k = $i - 1; while( $k &gt;= 0) { if( count( array_diff( $answer[$k], $answer[$i])) == 0) { // This array is the same as a previous one, start over $answer[$i] = array(); $j = -1; break; } $k--; } // Optionally sort each array sort( $answer[$i]); } } } var_dump( $answer); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload