douji0588 2013-10-21 12:14
浏览 40
已采纳

在php中避免重复随机数组输出

I'm currently working on a school project about php. The task is to design a website with some php navigation, a calculator, a truthsayer and some other functions. Now, I have a problem with the truthsayer I would like to get some help with. The truthsayer is a php randomizer that prints out a "truth" from an array I've set up. The truthsayer also prints out different amounts of truths depending on what number I give it. For example: If I type "3" into a box and presses enter, it will post three truths.

Now the problem: Since it completely randomizes it's output, I tend to get the same truths after each other. I for example ask for 10 truths, and 4-5 of them are the same as one of the other truths. Therefore I was wondering if anyone had an idea on how to make it avoid posting the same truth over and over again. Someone I know suggested a temporary removal of the truth from the array, but I'm not sure how I will make that work.

Any ideas? Much appreciated=P

My code:

<html>
<body>


<div id="Sannsigar">
<?php

if(isset($_POST["psc"])) {
$adder1 = $_POST["i1"];

$SannOutput= "";

    $Val = array("1=Ichi","2=Ni","3=San","4=Shi/Yon","5=Go","6=Roku","7=Nana","8=Hachi","9=Kyu","10=Jyu");

    switch ($adder1) {

        case "1" : {
        for ($x=1; $x<=$adder1; $x++) 
        {
        echo $Val[array_rand($Val)] . "<br>"; }
        "<br>";
        break;
        }

        case "2" : {
        for ($x=1; $x<=$adder1; $x++) 
        {
        echo $Val[array_rand($Val)] . "<br>"; }
        break;
        }

        case "3" : {
        for ($x=1; $x<=$adder1; $x++) 
        {
        echo $Val[array_rand($Val)] . "<br>"; }
        break;
        }

        case "4" : {
        for ($x=1; $x<=$adder1; $x++) 
        {
        echo $Val[array_rand($Val)] . "<br>"; }
        break;
        }

        case "5" : {
        for ($x=1; $x<=$adder1; $x++) 
        {
        echo $Val[array_rand($Val)] . "<br>"; }
        break;
        }

        case "6" : {
        for ($x=1; $x<=$adder1; $x++) 
        {
        echo $Val[array_rand($Val)] . "<br>"; }
        break;
        }

        case "7" : {
        for ($x=1; $x<=$adder1; $x++) 
        {
        echo $Val[array_rand($Val)] . "<br>"; }
        break;
        }

        case "8" : {
        for ($x=1; $x<=$adder1; $x++) 
        {
        echo $Val[array_rand($Val)] . "<br>"; }
        break;
        }

        case "9" : {
        for ($x=1; $x<=$adder1; $x++) 
        {
        echo $Val[array_rand($Val)] . "<br>"; }
        break;
        }

        case "10" : {
        for ($x=1; $x<=$adder1; $x++) 
        {
        echo $Val[array_rand($Val)] . "<br>"; }
        break;
        }


    default: {
    Echo "FEIL! Feil inndata. Oppgje eit tal mellom 1 og 10.";
    break;
    }

}

$Footer = fopen("Sannsigar/footer.php", "w");
fwrite ($Footer, "$adder1 sanning(ar) fr� Birger AS" . "<br>");
fclose($Footer);

}
?>

</div>

</body>
</html>
  • 写回答

1条回答 默认 最新

  • dtja73027 2013-10-21 12:20
    关注

    You don't need to define a switch case for each input. You can achieve the same thing using a little function.

    function getRandom($adder1, $Val) {
        $randoms = range(0, $adder1-1);
        shuffle($randoms);
        if ($adder1 <= count($Val)) {
            foreach ($randoms as $r) {
                echo $Val[$r]."
    ";
            }
        }
    }
    

    The function can be called as below:

    getRandom(3, $Val);
    

    Sample Output:

    9=Kyu
    3=San
    10=Jyu
    

    Demo!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序