php獲取一定范圍內(nèi)取N個不重復(fù)的隨機數(shù)

字號:


    本文實例講述了php獲取一定范圍內(nèi)取N個不重復(fù)的隨機數(shù)的方法。分享給大家供大家參考,具體如下:
    //range 是將1000到9999 列成一個數(shù)組
    $numbers = range (1000,9999);
    //shuffle 將數(shù)組順序隨即打亂
    shuffle ($numbers);
    //array_slice 取該數(shù)組中的某一段
    $result = array_slice($numbers,0,3);
    print_r($result);
    運行結(jié)果為:
    Array
    (
      [0] => 9767
      [1] => 2344
      [2] => 7783
    )
    希望本文所述對大家PHP程序設(shè)計有所幫助。