Reputation: 11
I am in a class at IUPUI and I was given these instructions
At the very top of the page, add the following PHP code block and comments.
<?php
/*
* Author: Your name
* Date: Today’s date
*/
?>
Inside the body section, create a H2 heading that reads “Statistical analysis of results from rolling a six‐sided die” at the beginning of the page body.
$frequency1
, $frequency2
……To simulate rolling a die, use the built‐in PHP function named rand($min, $max)
to generate a
random number between 1 and 6, 1 being Face 1, 2 being Face 2, and so on. The following statement
generates a random number between 1 and 6 and stores it in the variable called $face
. $face = rand(1, 6);
Use a conditional structure (IF or SWITCH) to increment frequency for each side of the die occurred.
For example, if the value of $face
is 1, increment $frequence1 by 1.
Below the table, add a refresh button.
<input type="submit" value="Refresh" onclick="window.location.reload()" />
Add CSS to center everything on the page.
I'm not asking for someone to do the work for me because I have most of the work done. I can't seem to figure out how to make my code loop 5000 times. This is my code currently.
<!DOCTYPE html>
<html>
<head>
<title>Statistical analysis of results from rolling a six‐sided die</title>
</head>
<body>
<h2>Statistical analysis of results from rolling a six‐sided die</h2>
<?php
$frequency1=0;
$frequency2=0;
$frequency3=0;
$frequency4=0;
$frequency5=0;
$frequency6=0;
$face = rand(1, 6);
if ($face==1)
{
++$frequency1;
}
else if ($face==2) {
++$frequency2;
}
else if ($face==3) {
++$frequency3;
}else if ($face==4) {
++$frequency4;
}else if ($face==5) {
++$frequency5;
}else if ($face==6) {
++$frequency6;
}
echo "<table>
<tr>
<th>Face</th>
<th>Frequency</th>
</tr>";
$face_num=6;
$face_count=1;
while ($face_count<=$face_num) {
$frequency = ${'frequency' . $face_count};
echo "<tr>
<td> $face_count </td>
<td> $frequency </td>
</tr>";
$face_count++;
}
?>
</table>
<input type="submit" value="Refresh" onclick="window.location.reload()" />
</body>
</html>
Upvotes: 0
Views: 761
Reputation: 6693
You can use a for()
loop to execute the code 5000 times.
$frequency = [0, 0, 0, 0, 0, 0];
for($i = 1; $i <= 5000; $i++):
$face = rand(0, 5); // index's start at 0
++$frequency[$face];
endfor;
for($i = 0; $i <= count($frequency) -1; $i++):
$n = $i +1;
echo "Frequency {$n} is equal to {$frequency[$i]}";
endfor;
for readability, I added the use of an array so its easier to manage in the future.
Running this code 10 times (not 5000) gives a result like this:
Frequency 1 is equal to 1
Frequency 2 is equal to 3
Frequency 3 is equal to 1
Frequency 4 is equal to 3
Frequency 5 is equal to 1
Frequency 6 is equal to 1
Note: You will need to implement this into your own view.
Here is an example of a view:
$frequency = [0, 0, 0, 0, 0, 0];
echo "<table>";
for($i = 1; $i <= 10; $i++):
$face = rand(0, 5);
++$frequency[$face];
echo "<tr>";
echo "<th>";
echo "Face";
echo "</th>";
echo "<th>";
echo "Frequency";
echo "</th>";
echo "</tr>";
echo "<tr>";
echo "<th>";
echo "{$face}";
echo "</th>";
echo "<th>";
echo "{$frequency[$face]}";
echo "</th>";
echo "</tr>";
endfor;
echo "</table>";
Upvotes: 1