■ 백엔드 ■/PHP

rand( [int $min, int $max] )

serpiko 2015. 3. 3. 11:54

int rand ( [int $min, int $max]) 


PHP에서 난수를 발생시키는 랜덤함수.


여기서 기본 인자인 min,max를 생략하고 그냥 rand()를 사용하면


0과 getrandmax() 까지의 임의의 난수를 반환하게 된다.


* (윈도우 기준) getrandmax() 는 오직 32767까지의 값을 가진다. 만약 더 큰 수를 지정하고 싶다면 정수로 직접 적어주길 바란다.


만약 5~15까지의 임의의 수를 얻고싶다면 rand(5, 15) 로 사용한다.


예제) 랜덤하게 열리는 페이지


<?


$url_cnt = rand(0,4);


switch ($url_cnt){ 

case "0";

echo "<script>location.href = 'index/main0.html';</script>";

     exit; 

     break;


    case "1": 

     echo "<script>location.href = 'index/main1.html';</script>";

     exit; 

     break; 


    case "2": 

     echo "<script>location.href = 'index/main2.html';</script>";

     exit; 

     break; 


   case "3": 

     echo "<script>location.href = 'index/main3.html';</script>";

     exit; 

     break;

   case "4": 

     echo "<script>location.href = 'index/main4.html';</script>";

     exit; 

     break;

   } 


?>





참고 : http://php.net/manual/en/function.rand.php