简体   繁体   English

PHP的循环随机数生成器的问题

[英]php for loop random number generator issue

Task: 任务:

  1. Use a for loop to build an array (randArray[]) with 50 random integers ranging from 0 - 100. 使用for循环来构建一个数组(randArray []),其中包含50个随机整数,范围从0到100。
  2. Use a while loop to build a new array (oddArray[]) with all the odd numbers in randArray[] 使用while循环使用randArray []中的所有奇数构建一个新数组(oddArray [])。
  3. Use foreach loop to print all the odd numbers in oddArray[]. 使用foreach循环可打印oddArray []中的所有奇数。

I just can't understand why this first step for loop doesn't work 我只是不明白为什么循环的第一步不起作用

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test1-1</title>
</head>
<body>
    <?php
        $randArray= [];
        $value = [];
        for ($randArray= 0; $randArray<= 50; $randArray++) {
                $value = rand(0, 100)
                $randArray[] = $value
            }
        endfor;
        echo ($randArray);
    ?>
</body>
</html>

You can skip a few of them requirements and just handle odd numbers, also rand is not that random.. 您可以跳过其中的一些要求,而只处理奇数,而rand并不是那么随机。

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test1-1</title>
</head>
<body>
    <?php
    for ($i= 0; $i<= 50; $i++) {
        $value = mt_rand(0, 100);

        // odd number
        if ($value % 2 != 0) {
           echo $value.'<br>';
        } 
    }
    ?>
</body>
</html>

Edit (some comments with the syntax issues on original code) 编辑(一些有关原始代码语法问题的注释)

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test1-1</title>
</head>
<body>
    <?php
    // $randArray set as array, but used as int
    $randArray= [];
    // $value set as array, but used as int
    $value = [];

    // $randArray is now set as an int
    for ($randArray= 0; $randArray<= 50; $randArray++) {
            $value = rand(0, 100) // <-- missing semi-colon + rand not that random, mersenne twister is better
            $randArray[] = $value // <-- missing semi-colon + treating $randArray as an array when its now an int, scala value
        }
    endfor; // whats this?

    // $randArray would be an array so you cant just echo it
    echo ($randArray);

    ?>
</body>

Abiding by the instructions, you can do this 遵守说明,您可以执行此操作

// random and odd arrays
$randArray = [];
$oddArray = [];

// For loop to get 50 random integers and push to randArray
for ($i = 0; $i <= 50; $i++) {
    $value = rand(0, 100);
    $randArray[] = $value;
}

// While loop to get odd numbers from randArray and push to oddArray
$x = 0;
while ($x <= 50) {
    if ($randArray[$x] % 2 == 1) {
        $oddArray[] = $randArray[$x];
    }
    $x++;
}

// Foreach loop to print all odd numbers
foreach($oddArray as $odds) {
    echo $odds . '<br>';
}

Side notes: Use different variables as for loop arguments. 旁注:对于循环参数,请使用其他变量。 That's highly what's causing you errors. 这就是导致您出错的高度原因。 You used randArray again for executing for loop 您再次使用randArray执行for循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM