Jump to content

Random in array????


Eagle710

Recommended Posts

Why don't you check when putting the random number into the array?

 

<?php
$rn = array();
for ($i=0;$i<100;$i++) {
    $tst = rand(1,1000);
    while (in_array($tst,$rn)) {
        echo $tst . ' is already in the array<br>';  // debug -- check
        $tst = rand(1,1000);
    }
    $rn[$i] = $tst;
}
echo '<pre>' . print_r($rn,true) . '</pre>';
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/92032-random-in-array/#findComment-471339
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.