PHP Help with Die Roll Frequency 5000 Times

I am doing an assignment and I need some pointers on how to roll a die a total of 5000 times. I am not sure what to do next to make the die table work! Here is my code so far:

[php]

<?php $faceside1 = 1; $faceside2 = 2; $faceside3 = 3; $faceside4 = 4; $faceside5 = 5; $faceside6 = 6; //Frequency of Die $frequency1 = 1; $frequency2 = 2; $frequency3 = 3; $frequency4 = 4; $frequency5 = 5; $frequency6 = 6; $face = rand(1, 6); switch ($face) { case "1": echo $frequency1++; break; case "2": echo $frequency2++; break; case "3": echo $frequency3++; break; case "4": echo $frequency4++; break; case "5": echo $frequency5++; break; case "6": echo $frequency6++; break; default : echo "0"; } ?>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>Statistical analysis of results from rolling a six‐sided die</title>
        </head>
        <body>
            <h2 style="text-align:center">Statistical analysis of results from rolling a 
                six‐sided die</h2>
    <table width='600' border='1' cellspacing='0' cellpadding='5' align='center'>
        <tr>
            <th>Face</th>
            <th>Frequency</th>
        </tr>
        <?php
        define("MAXIMUM_TOTAL", 6);


        for ($frequencytotal = 1; $frequencytotal <= MAXIMUM_TOTAL; $frequencytotal++) {
            $faceside = ${'faceside' . $frequencytotal};
            $frequency = ${'frequency' . $frequencytotal};

            echo "<tr>
            <td> $faceside </td>
            <td> $frequency </td>
            </tr>";
        }
        ?>
</body>
[/php]

I am not sure where to add the frequency of 5000 and I am having the random number in the top left corner on the webpage as well. I have all the numbers in the table tho, but I am stuck right now on what I should change or do next. We have not learned arrays yet, so that is something I cannot add! Any help is greatly appreciated!

Try to keep all you php at the top, by this I mean your calculations and only have the minimum php in the HTML (mostly for displaying).

I know I shouldn’t do this, but here’s how would go about doing this. (To bad you can’t use arrays).

[php]<?php
$faceside1 = 1;
$faceside2 = 2;
$faceside3 = 3;
$faceside4 = 4;
$faceside5 = 5;
$faceside6 = 6;

//Frequency of Die
$frequency1 = 0;
$frequency2 = 0;
$frequency3 = 0;
$frequency4 = 0;
$frequency5 = 0;
$frequency6 = 0;

$total = 2000;

for ($x = 1; $x <= $total; $x++) {
$die = rand(1, 6);

switch ($die) {
    case 1:
        $frequency1++;
        break;
    case 2:
        $frequency2++;
        break;
    case 3:
        $frequency3++;
        break;
    case 4:
        $frequency4++;
        break;
    case 5:
        $frequency5++;
        break;
    case 6:
        $frequency6++;
        break;
    default :
        echo 0;
}

}

$results = "Side One: " . $frequency1 . " Side Two: " . $frequency2 . " Side Three: " . $frequency3 . " Side Four: " . $faceside4 . " Side Five: " . $faceside5 . " Side Six: " . $faceside6;
?>

Frequency

Statistical analysis of results from rolling a six‐sided die

<?php echo $results; ?>

[/php]

I didn’t put it in a Table for you, for I figure should be able to figure that out (It should be easy). Plus there’s a few other things that probably should be done better, especially if it’s an assignment. :wink:

Thanks for your help. We haven’t gotten to arrays yet, so that wouldn’t have worked. I also went to the tutor and with both of you, I got it figured out, so I appreciate your help. I went on another forum and they were not so nice about helping me, so you are awesome!!!

Sponsor our Newsletter | Privacy Policy | Terms of Service