Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 506
user1761116 Avatar asked Dec 11 '22 20:12

user1761116


1 Answers

you can use this function:

Yii::app()->getSecurityManager()->generateRandomString($length);

Documentation of this function can be found here.

like image 149
Saleh Hashemi Avatar answered Dec 17 '22 17:12

Saleh Hashemi