脚本宝典收集整理的这篇文章主要介绍了php – 无重复的随机数生成,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我的代码是,
这里,long1,long2,… long10是数据库中的目录名
$array=array(); for($n=1;$n<=$long;$n++) { $files = array(); $dir=${'long'.$n}; if(is_dir($dir)) { $opendir = oPEndir($dir); while (false !== ($file = readdir($openDir))) { if ($file != "." && $file != "..") { $files[] = $file; } } closedir($openDir); } mt_srand((double) microtime()*1000000); $randnum = mt_rand(0,(sizeof($files)-1)); $arraycount=count($array); for($index=0;$index<=$arraycount;$index++) { if(!in_array($array,$randnum)) { $array[]=$randnum; } } $img = $dir."/".$files[$randnum]; <input type="image" class="advt_image" src="<?=$img;?>" alt="" name=""/> }
例如:如果在数据库中设置了7个横幅,我必须显示来自不同或相同文件夹的7个横幅.(某些横幅将来自同一个文件夹).每当我显示网页时,我都需要避免重复的横幅.
我已经分配了一个数组来存储每个随机数.我需要更改代码中的任何内容吗?任何想法/想法?
谢谢!
$files = array(...); // this holds the files in the directory $banners = array(); // this will hold the files to display $count = 7; for($i=0;$i<$count;$i++) { $c = mt_rand(0,count($files)); $banners[] = $files[$c]; $files = array_diff($files,array($files[$c])); } // Now go ahead and display the $banners
以上是脚本宝典为你收集整理的php – 无重复的随机数生成全部内容,希望文章能够帮你解决php – 无重复的随机数生成所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。