ARRAY_RAND始终具有相同的输出

时间:2013-11-25 23:20:56

标签: php arrays

这是什么我在php中有一个任务,我需要随机选择一张卡,然后是一个名为“draw”的按钮,它可以抓取另一张随机卡片。

            <td><b>Rectangle 10</b></br>

        <p>Your card is: </p>

        <?php
        $cards = array("AC","AD","AH","AS","1C","1D","1H","1S",
        "2C","2D","2H","2S","3C","3D","3H","3S","4C","4D","4H","4S",
        "5C","5D","5H","5S","6C","6D","6H","6S","7C","7D","7H","7S",
        "8C","8D","8H","8S","9C","9D","9H","9S","10C","10D","10H","10S",
        "JC","JD","JH","JS","QC","QD","QH","QS","KC","KD","KH","KS"
        );
        $card = array_rand($cards, '1');
        print_r($cards[$card]);
        ?>

        <form>
        <input type="button" value="Draw" onClick="window.location.reload()">
        </form>

        </td>

代码可以自行运行,但在整个页面中它没有,我现在将粘贴整页。

<?php
$today = date_default_timezone_set('Europe/London');
$a = 3034;
srand($a);
?>

 <table border='1'>
    <tr>
        <td><b>Rectangle 1</b></br>Rob Dorsett </br> 12007071 </br><?php  echo $today = date('d/m/y')?></td>
        <td><b>Rectangle 2</b></br><?php echo $addition = (3+4+5) ?></td>
    </tr>

    <tr>
        <td><b>Rectangle 3</b></br> <?php srand(3034);
        echo (rand()); ?> </td>

        <td><b>Rectangle 4</b></br>
        <?php for ($i=30;$i<=40;$i++){
            if($i%2 == 0) {
            echo $i. '</br>';}
            }
        ?>
        </td>
    </tr>


    <tr>
        <td><b>Rectangle 5</b></br> 

        <form>
        </br>
        Enter four digits: </br>
        <input name="input" />
        </br><input type="submit" value="Sumbit" /> 
        </form>
        <?php
        $x = $_GET['input'];
        ?>
        </td>

        <td><b>Rectangle 6</b></br>

        <?php
        if ($x%2 == 0){
        echo 'Your number is even';
        }
        else 
        {
        echo 'Your number is odd';
        }
        ?>
        </td>

    </tr>

        <td><b>Rectangle 7</b></br>
            <form>
            Enter a seed for a random number: </br>
            <input type="text" name="random" /> </br>
            <input type="submit" value="Generate" /> </br>
            <?php
            $r7 = $_GET['random'];
            srand($r7);
            echo rand();                
            ?>
            </form>


        </td>
        <td><b>Rectangle 8</b></br>
        <?php
        echo str_rot13('Example Rot13');
        ?>
        </br>
        <i>Example Rot13</i>

        </td>
    <tr>
        <td><b>Rectangle 9</b></br>

        <script type="text/javascript">
function rot13 (txt) {
 var map = []
 var tmp = "abcdefghijklmnopqrstuvwxyz"
 var buf = ""

for (j = 0; j < tmp.length; j++) {
    var x = tmp.charAt(j); var y = tmp.charAt((j + 13) % 26)
    map[x] = y; map[x.toUpperCase()] = y.toUpperCase()
  }

for (j = 0; j < txt.length; j++) {
    var c = txt.charAt(j)
    buf += (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' ? map[c] : c)
}

return buf
}
        </script>

        <script>var foo = rot13 ('Example Rot13')</script>
        <p>Encoded: <script>document.write(foo)</script>
        <p>Decoded: <script>document.write(rot13(foo))</script>

        </td>


        <td><b>Rectangle 10</b></br>

        <p>Your card is: </p>

        <?php
        $cards = array("AC","AD","AH","AS","1C","1D","1H","1S",
        "2C","2D","2H","2S","3C","3D","3H","3S","4C","4D","4H","4S",
        "5C","5D","5H","5S","6C","6D","6H","6S","7C","7D","7H","7S",
        "8C","8D","8H","8S","9C","9D","9H","9S","10C","10D","10H","10S",
        "JC","JD","JH","JS","QC","QD","QH","QS","KC","KD","KH","KS"
        );
        $card = array_rand($cards, '1');
        print_r($cards[$card]);
        ?>

        <form>
        <input type="button" value="Draw" onClick="window.location.reload()">
        </form>

        </td>   
    </tr>
</table>    

2 个答案:

答案 0 :(得分:3)

您正在为srand()函数提供静态值,因此您将始终获得相同的结果。另外,看看我上次的评论。

答案 1 :(得分:2)

移除所有支架();让你的代码工作。

您应该创建一些函数来获取随机数并将PHP与html / js分开。这有助于提高可读性。