Return to Snippet

Revision: 48574
at July 5, 2011 12:11 by mawady


Updated Code
<?php
    function GetUnrepeatedRandomVariables($minIndex,$maxIndex)
    {
        $res = array();

        while(1)
        {
            if(count($res) == ($maxIndex-$minIndex+1))
                break;
            $rand = rand($minIndex, $maxIndex);
            if(!in_array($rand, $res))
                array_push ($res, $rand);
        }
        return $res;
    }

    $arr = GetUnrepeatedRandomVariables(0, 10);        
    print (count($arr) ."<br />");
    foreach ($arr as $val)
        print($val ."<br />");
?>

Revision: 48573
at July 5, 2011 12:09 by mawady


Initial Code
<?php
        function GetUnrepeatedRandomVariables($minIndex,$maxIndex)
        {
            $res = array();

            while(1)
            {
                if(count($res) == ($maxIndex-$minIndex+1))
                    break;
                $rand = rand($minIndex, $maxIndex);
                if(!in_array($rand, $res))
                    array_push ($res, $rand);
            }
            return $res;
        }

        $arr = GetUnrepeatedRandomVariables(0, 10);        
        print (count($arr) ."<br />");
        foreach ($arr as $val)
            print($val ."<br />");
?>

Initial URL


Initial Description
Generating Array of Un-repeated  Random Variables In User-Defined Bounds

Initial Title
Un-repeated  Random Variables

Initial Tags


Initial Language
PHP