0
    <body>
<?php


$face1 = "1";
$face2 = "2";
$face3 = "3";
$face4 = "4";
$face5 = "5";
$face6 = "6";

$frequency1 = 0;
$frequency2 = 0;
$frequency3 = 0;
$frequency4 = 0;
$frequency5 = 0;
$frequency6 = 0;
$result = rand(1, 6);

if ($result = 1)
    $frequency1+=1;
if ($result = 2)
    $frequency2+=1;
if ($result = 3)
    $frequency3+=1;
if ($result = 4)
    $frequency4+=1;
if ($result = 5)
    $frequency5+=1;
if ($result = 6)
    $frequency6+=1;
?>


        <h2>Statistical analysis of results from rolling a
six‐sided die</h2>

        <table width='600' border='1' cellspacing='0' cellpadding='0' align='center'>
            <tr>
                <th>Face</th>
                <th>Frequency</th>
            </tr>
            <?php
            define("FACE_NUM", 6);
            $face_count=1; 
            while ($face_count<=FACE_NUM) {
                $number = ${'face' . $face_count};
                $frequency = ${'frequency' . $face_count};

                echo "<tr>
                        <td> $number </td>
                        <td> $frequency </td>
                     </tr>";

                $face_count++;
                }
            ?>
        </table>
        <input type="submit" value="Refresh" onclick="window.location.reload()" />  

    </body>
</html>

When I test it in a browser, it only shows the number 1 in the column where it shows the frequency. How do I make it roll 1,000 times and then display the frequencies? What am I missing?

We're supposed to use at least one loop, so that is required in the answer. The frequencies of each number on the die need to be displayed in a table.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • 2
    You're assigning instead of comparing - and you need to do the roll and increment in the loop – Qirel Sep 21 '17 at 22:45
  • Here's an example of how you can do the loop: https://3v4l.org/1oOIT then it's just a matter of displaying it. – Qirel Sep 21 '17 at 22:51

2 Answers2

4

I would like to solve your question from scratch.

$freqs = [0,0,0,0,0,0]; // array in which you save the frequency of each face

for ($i = 0; $i < 1000; ++$i) {
    ++$freqs[rand(0,5)];
}

// then just print the array freqs (by dividing the values with 1000 if needed
jack
  • 1,391
  • 6
  • 21
1

The actual error:

One immediate error is in this line (and all the similar lines):

if ($result = 4)

The single = operator is assignment, so you're not checking if $result equals 4, but you're assigning 4 to $result. The outcome of that assignment is also 4, which evaluates to true, so you will increase $frequency 4 always, regardless of the initial value of $result.

The missing loop(s)

To execute a 1000 throws, you should't have 1000 copies of your code, but just have a loop. You could even introduce more loops, because you also have the same code for each of the faces, and with a loop you wouldn't need to copy that code 6 times. What if you would change the program for a 20 faced dice? It would become huge! Instead use a loops and arrays.

The added advantage is that your program is way more flexible. You only have to change one number to make it work for odd dice with more faces. You could even ask the user how many faces his dice has and put that number in the $faces variable.

Here some example code. It could maybe be shorter, because you could initialize the result array with some PHP magic, but written out in full it's still pretty compact, and hopefully more clear what's going on.

// Config. Hard coded numbers now, next step is to read them from $_GET.
$faces = 6;
$throws = 1000;

// Initialize results. It's an array where the face number is the index.
for($f = 1; $f <= $faces; $f++) {
  $results[$f] = 0;
}

// Do a lot of throws
for ($t = 0; $t < $throws; $t++) {
  // The actual throw.
  $face = rand(1, 6);
  // Incrementing the counter for that face.
  $results[$face]++;
}

// Print the results
echo "Result of throwing a $faces faced dice $throws times:";
print_r($result);
GolezTrol
  • 114,394
  • 18
  • 182
  • 210