久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      分享php生成不重復(fù)隨機(jī)數(shù)、數(shù)組的4種方法

      分享php生成不重復(fù)隨機(jī)數(shù)、數(shù)組的4種方法

      下面寫幾種生成不重復(fù)隨機(jī)數(shù)的方法,直接上代碼吧

      代碼如下:

      <?phpdefine('RANDOM_MAX', 100);define('COUNT', 10); echo 'max random num: '.RANDOM_MAX, ' ;result count:'.COUNT, '<br/>'; invoke_entry('rand1');invoke_entry('rand2');invoke_entry('rand3');invoke_entry('rand4'); function invoke_entry($func_name) { $time = new time(); $time->time_start(); call_user_func($func_name); echo $func_name.' time spend: ', $time->time_spend(); echo '<br/>';}function rand1() { $numbers = range (1, RANDOM_MAX); shuffle($numbers); //隨機(jī)打亂數(shù)組 $result = array_slice($numbers, 1, COUNT); return $result;}function rand2() { $result = array();  while(count($result)< COUNT) {  $result[] = mt_rand(1, RANDOM_MAX); //mt_rand()是比rand()更好更快的隨機(jī)函數(shù)  $result = array_unique($result); //刪除數(shù)組中重復(fù)的元素 } return $result;}function rand3() { $result = array();    while(count($result) < COUNT) {  $_tmp = mt_rand(1, RANDOM_MAX);  if(!in_array($_tmp, $result)) { //當(dāng)數(shù)組中不存在相同的元素時(shí),才允許插入   $result[] = $_tmp;  } }    return $result;}function rand4() { $result = array(); while (count($result) < COUNT) {  $result[] = mt_rand(1, RANDOM_MAX);  $result = array_flip(array_flip($result)); //array_flip將數(shù)組的key和value交換 } return $result;}class time { private $_start;  public function time_start() {  $this->_start = $this->microtime_float(); } public function time_spend() {  return $this->microtime_float() - $this->_start; } private function microtime_float() {  list($usec, $sec) = explode(" ", microtime());  return ((float)$usec + (float)$sec); }} ?>

      說一下第四種方法,就是翻翻法了,利用array_flip()將數(shù)組的鍵和值翻轉(zhuǎn),利用php數(shù)組特性,重復(fù)的鍵會(huì)覆蓋,此時(shí)再翻轉(zhuǎn)一次,就相同于去掉了重復(fù)的值。
      以上幾種方法只是簡單的例子,有的方法適用范圍有限。

      在看看幾種方法的效率:

      分享php生成不重復(fù)隨機(jī)數(shù)、數(shù)組的4種方法

      分享php生成不重復(fù)隨機(jī)數(shù)、數(shù)組的4種方法

      array_unique()在數(shù)組較大時(shí)性能比較差,當(dāng)然shuffle()也會(huì)受此影響。

      相關(guān)學(xué)習(xí)推薦:PHP編程從入門到精通

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)