php知识案列1

PHP,在1-20间随机产生5个不重复的值,如何做

复制代码 代码如下:

<?php function NoRand($begin=0,$end=20,$limit=5){ $rand_array=range($begin,$end); shuffle($rand_array);

//调用现成的数组随机排列函数 return array_slice($rand_array,0,$limit);

//截取前$limit个 } print_r(NoRand()); ?>

 

或者不shuffle的话

复制代码 代码如下:

<?php $tmp=array();

while(count($tmp)<5){ $tmp[]=mt_rand(1,20);

$tmp=array_unique($tmp); } print join(',',$tmp); ?>

 

原文地址:https://www.cnblogs.com/xa4312cs/p/6124095.html