Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I know it's not a good idea just to provide code, but I was intrigued by this question. Here's how I would do it:</p> <pre><code>$d = 0.3; $random = array(); // Populate the original array for ($n=1; $n &lt;= 10; $n++) { $arr[] = $n; } $count = count($arr); // Loop through array foreach (array_keys($arr) as $key) { if (!isset($prev_key)) { $prev_key = array_rand($arr); } $possibles = array(); // This stores the possible values echo "Trying: $prev_key"; echo ":\n"; // Loop through the array again and populate $possibles with all possible // values based on the previous values foreach (array_keys($arr) as $n) { if ($arr[$n] &lt; $prev_key - $count * $d || $arr[$n] &gt; $prev_key + $count * $d) { $possibles[] = $n; echo $arr[$n]." is valid\n"; } else { echo $arr[$n]; echo " outside range\n"; } } // If there is nothing outside that range, just return the remaining values if (count($possibles) == 0) { $possibles = array_keys($arr); echo "Nothing within range so just returning whole array\n"; } echo "\n"; // Choose random value from the possible values array $rand_key = $possibles[array_rand($possibles)]; $random[] = $arr[$rand_key]; $prev_key = $arr[$rand_key]; // Unset this value from the original array since we can only use the // values once unset($arr[$rand_key]); } print_r($random); </code></pre> <p>This will produce output like this:</p> <pre><code>Trying: 8: 1 is valid 2 is valid 3 is valid 4 is valid 5 outside range 6 outside range 7 outside range 8 outside range 9 outside range 10 outside range Trying: 2: 1 outside range 3 outside range 4 outside range 5 outside range 6 is valid 7 is valid 8 is valid 9 is valid 10 is valid Trying: 9: 1 is valid 3 is valid 4 is valid 5 is valid 6 outside range 7 outside range 8 outside range 10 outside range Trying: 5: 1 is valid 3 outside range 4 outside range 6 outside range 7 outside range 8 outside range 10 is valid Trying: 10: 1 is valid 3 is valid 4 is valid 6 is valid 7 outside range 8 outside range Trying: 4: 1 outside range 3 outside range 6 outside range 7 outside range 8 is valid Trying: 8: 1 is valid 3 is valid 6 outside range 7 outside range Trying: 3: 1 outside range 6 outside range 7 is valid Trying: 7: 1 is valid 6 outside range Trying: 1: 6 is valid Array ( [0] =&gt; 2 [1] =&gt; 9 [2] =&gt; 5 [3] =&gt; 10 [4] =&gt; 4 [5] =&gt; 8 [6] =&gt; 3 [7] =&gt; 7 [8] =&gt; 1 [9] =&gt; 6 ) </code></pre> <p>The only drawback is that since it randomly gets rows, there is a chance that the values near the end may not be outside the defined range. By my tests, this happens to about 4% using the above <code>$d = 0.25</code> and 1000 values. One way to get around this is just to insert these values back in at random places instead of appending them like I have done.</p> <p>Also note, this method is not that efficient. It has to loop through the array <code>count($arr) ^ 2</code> times. So for 1000 values, you're looking at 1,000,000 iterations. Fortunately the array gets progressively smaller.</p>
    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.
    3. 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