Non-sequential Iterating in PHP
7 Nov
I needed to iterate through an unknown amount of records, but I need to do it in a non-sequential manner, as to appear that I was not enumerating the data set in its entirety.
So how could someone enumerate every single record, non-sequential, without duplication, indefinably? I modified my Fisher Yates shuffle to do this exact task:
function nonseq($amount,$iter=0,$seed=NULL) {
if (!$seed) {
list($usec, $sec) = explode(' ', microtime());
$seed = (float) $sec + ((float) $usec * 100000);
}
srand($seed);
for($i=0;$i<$amount;$i++) $map[$i] = ($i + 1);
for($i=(count($map)-1);$i>=0;$i--) {
$j = @rand(0,$i);
$tmp = $map[$i];
$map[$i] = $map[$j];
$map[$j] = $tmp;
}
if ($iter > ($amount)) {
$base = floor($iter / $amount);
$niter = $iter - ($base * $amount);
$prec = nonseq($amount,($niter+1),$seed+1);
return $prec + ($base * $amount);
}
return $map[$iter-1];
}
This function takes 3 parameters, but two are optional. The first, and required, parameter is increment partition size. The second parameter is the normal sequential pointer. If you do not provide a second parameter then it will default to 0. The third parameter is a random seed.
Logic of the function:
X = Increment Partition Size, Y = Sequential Pointer, Z = Random Seed.
If (X >= Y), results = (FYShuffled number between 0 and X randomed by Z)
If (X < Y ), results = ((FYShuffled number between 0 and X randomed by ( Z+(X/Y) )) + (largest multiple of X that is less than Y))
<?
include("nonseq.php");
echo nonseq(10,50,5);
?>
The above examples first parameter instructs the function to randomize sequential in increments of 10. The second value asks what the 50th sequential iteration would be converted to, and the 5 is a simple random seed. Since the first value is 10, then we can assure that the output will be between 51 and 60, and in my case was : 60. If we run the same function with the sequential iteration set to 51 we get : 54.
So if you were to normally iterate through records as such :
for($i=1;$i<=3000;$i++) {
$cont = file("http://www.scrape-site.com/sites.php?id={$i}");
record_info($cont);
}
This example would iterate sequentially ( id=1, id=2, id=3, id=4, id=5...)
However, if you use the nonseq function :
for($i=1;$i<=3000;$i++) {
$nsid = non_seq(1000,$i,5);
$cont = file("http://www.scrape-site.com/sites.php?id={$nsid}");
record_info($cont);
}
This would iterate in a non-sequential manner(id=832, id=272, id=698, id=177...). It would continue and without repeating any numbers indefinably.

