Note Submitter: php at ngaru dot com
----
Here is a function that selects a random bunch of rows from a database statement. The getdata() function returns a multidimensional array of data (e.g. - just to explain - dont take this literally)
$data[0] = array("ID"=>1, "name", "test row 1");
$data[1] = array("ID"=>2, "name", "test row 2");
function randomselection($sql, $c, $withoutreplacement = 1) {
//$c = count of rows to be selected
$data = getdata($sql);
$totalcount = count($data);
$j = array();
if($totalcount < $c) {//not enough data points
return $data;
}
for($i = 0; $i < $c; $i++) {
$exists = 1
if($withoutreplacement) {
while($exists > 0 && $exists < 100) {
$j[$i] = floor(rand(0,$totalcount-1));
if(!in_array( $j[$i] , $j , 1 )) {
$exists = 0;
}
else {
$exists++;
}
}
}
else {
$j[$i] = floor(rand(0,$totalcount-1));
}
$newdata[$i] = $data[$j[$i]];
}
return $newdata;
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.