Help - Search - Members - Calendar
Full Version: 3 Random Ids Problem
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
Bogey
I need to generate 3 IDs to a dynamic maximum. If you don't know what I mean, it's going to look something like this.
CODE
$count = count($objects);

$id1 = rand(0,$count);
$id2 = rand(0,$count);
$id3 = rand(0,$count);

But I can't do it that way as I need those IDs to be different each time... any help on how I can do this? Maybe there is already a function to do this kind of thing... any help?

Thanks so much smile.gif
Bread
Try this on for size:

CODE
function get_rands($min, $max, $amount)
{
    $rands = array();
    $rand = false;
    
    for($i = 0; $i < $amount; $i++)
    {
        while(!$rand || in_array($rand, $rands))
        {
            $rand = rand($min, $max);
        }
        
        $rands[] = $rand;
    }
    
    return $rands;
}

// Usage
print_r(get_rands(1, 5, 5));


It's early morning here, so please excuse any glaring mistakes.

If you need something for unique random strings, have a look at http://www.php.net/uniqid
Bogey
Thanks so much Bread. That works with no blaring mistakes... thanks for the help smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.