技术宅

当前位置:首页 > 网站教程 > 代码学习

代码学习

PHP随机生成某彩号码的三种方法

时间:09-26 作者:
第一种<?php$red = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33);$blue = array(1,2,3,4,5,6,7,8,9,10,11,12,13,1

第一种

<?php
$red = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33);
$blue = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
for ($i=0;$i<6;$i++){
$index = rand(0,32-$i);
$redBall[]= $red[$index];
unset($red[$index]);
for($k=$index;$k<count($red)-1;$k++){
   $red[$k]=$red[$k+1];
}
}
asort($redBall);
?>
<div style="background-color:red;color:white;width:160px;float:left;text-align:center;">
<?php
foreach($redBall as $v){
echo $v." ";
}
?>
</div>
<div style="background-color:blue;color:white;width:40px;float:left;text-align:center">
<?php
echo $blue[rand(0,15)];
?>
</div>

第二种

<?php
echo "<br>";
echo "<p>";
$red1=range(1,33);
$blue1=mt_rand(1,6);
$sc1=array_rand($red1,6);
?>
<div style="background-color:red;color:white;width:160px;float:left;text-align:center;">
<?php
foreach($sc1 as $v){
echo "$v ";
}
?>
</div>
<div style="background-color:blue;color:white;width:40px;float:left;text-align:center">
<?php
echo "$blue1";
?>
</div> 

第三种

<?php
    $arr = array();
    $special = array();
    //1-32位号码
    for ($i = 1; $i <= 32 ; $i++){
        $arr[$i] = $i;
    }
    //1-15特别号码
    for ($i = 1; $i <= 15 ; $i++) { 
        $special[$i] = $i;
    }
    //随机取数组
    $arr2 = array_rand($arr,6);
    $arr3 = array_rand($special,1);
    
    $arr2[] = $arr3;

    $str = "<div style='font-size:16px;padding-top:10px;text-align:center'>";
    foreach($arr2 as $v){
        $str .= "<span style='color:red;font-size:25px'>$v---</span>";
    }
    $str .='</div>';
    
    echo $str;

?>