phpfunctionrandomyii

Yii random number generator function


In yii i am creating random number generating function. I am using function as-

public function randomNumber()
{
    //for generating random number
    $length =3;
    $chars = array_merge(range(0,9));
    shuffle($chars);
    $password = implode(array_slice($chars, 0,$length));
    echo $password;

}

but in above function its creating random number as per length specified. i.e. if length is defined as 2, then it will create 2 digit number,if 3 then 3 digit number and so on. Now my condition is i want to generate random number between 1 to 1000. It can be 1 digit, 2 digit or 3 digit. So how to modify this function? Or is there any other way? Please help me...


Solution

  • Something like this?

    <?php
    $length = rand(1,3);
    $chars = array_merge(range(0,9));
    shuffle($chars);
    $password = implode(array_slice($chars, 0,$length));
    echo $password;
    ?>
    

    But, actually, i think you must use it like this :)

    <?php
    $password = rand(1,1000);
    echo $password;
    ?>