Random number in range [min - max] using PHP

前端 未结 7 1558
走了就别回头了
走了就别回头了 2020-12-14 05:37

Is there a way to generate a random number based on a min and max?

For example, if min was 1 and max 20 it should generate any number between 1 and 20, including 1 a

7条回答
  •  旧巷少年郎
    2020-12-14 05:46

    Try This one. It will generate id according to your wish.

    function id()
    {
     // add limit
    $id_length = 20;
    
    // add any character / digit
    $alfa = "abcdefghijklmnopqrstuvwxyz1234567890";
    $token = "";
    for($i = 1; $i < $id_length; $i ++) {
    
      // generate randomly within given character/digits
      @$token .= $alfa[rand(1, strlen($alfa))];
    
    }    
    return $token;
    }
    

提交回复
热议问题