note 70844 added to function.rand
| From: | euan_m at yahoo dot fr | Date: | Tue, 31 Oct 2006 14:45:34 +0000 |
| Subject: | note 70844 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-119270@lists.php.net to get a copy of this message | ||
You could try this, it works fine :
<?php
$connection = mysql_connect ($host, $login, $password);
mysql_select_db ($DataBase, $connection) ;
$c = 5 ; // Whatever..
protected function randomselection($c, $connection) {
$data = getdata($connection) ; // Get your datas somewhere you like to
$totalcount = count($data) ;
$j = array() ;
if ($c > $totalcount) { $newdata = $data ; } // if not enough datas rows in $data
else {
for($i = 0; $i <= $c; $i++) {
$exists = 1 ;
for($exists > 0; $exists++;) {
$test = mt_rand(1,$totalcount) ; // depends on where you want to start - can also use rand()
if(!in_array( $test , $j )) { $j[$i] = $test ; $exists = 0 ; }
}
$newdata[$i] = $data[$j[$i]] ;
}
}
return $newdata ;
}
?>
----
Server IP: 194.246.101.61
Probable Submitter: 87.90.26.94
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- https://master.php.net/note/edit/70844
Del: integrated -- https://master.php.net/note/delete/70844/integrated
Del: useless -- https://master.php.net/note/delete/70844/useless
Del: bad code -- https://master.php.net/note/delete/70844/bad+code
Del: spam -- https://master.php.net/note/delete/70844/spam
Del: non-english -- https://master.php.net/note/delete/70844/non-english
Del: in docs -- https://master.php.net/note/delete/70844/in+docs
Del: other reasons-- https://master.php.net/note/delete/70844
Reject -- https://master.php.net/note/reject/70844
Search -- https://master.php.net/manage/user-notes.php