Jump to content

Rolling a Die 5000 times. Need Help!


Gaomon

Recommended Posts

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:

 

 

<!DOCTYPE html>
<?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>
</html>
 
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!
Edited by Gaomon
Link to comment
Share on other sites

$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";
        }
That is where you roll a die and track the result. Use a for loop to do that 5000 times.

 

There's also a problem with your code:

//Frequency of Die
$frequency1 = 1;
$frequency2 = 2;
$frequency3 = 3;
$frequency4 = 4;
$frequency5 = 5;
$frequency6 = 6;
If those variables count the number of times each face was rolled then shouldn't they start at 0?
Link to comment
Share on other sites

$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";
        }
That is where you roll a die and track the result. Use a for loop to do that 5000 times.

 

I tried putting it in my for loop and it didn't work. It gave me the same problem, but instead of only one number, it has given me a random 6 digit code underneath the title. 

 

 

 

 

"There's also a problem with your code:

//Frequency of Die

$frequency1 = 1;

$frequency2 = 2;

$frequency3 = 3;

$frequency4 = 4;

$frequency5 = 5;

$frequency6 = 6;If those variables count the number of times each face was rolled then shouldn't they start at 0?"

 

 

Here is what the part of my assignment says:  "Inside the PHP code block, create six variables to store the frequency of each side of the die. Choose your variable names wisely. For example, you may name them $frequency1, $frequency2 ……" so it doesn't start with 0. 

 

Finally I am not sure exactly where to put the 5000 at. I tried to put it in the Maximum Num Constant, but that didn't work, as it gave me 5000 "errors".

 

Thanks!

post-203492-0-33515800-1486720287_thumb.png

Link to comment
Share on other sites

I suggest you forget about PHP for now and just try to understand the basic logic. How does rolling a dice once work? What are the exact steps? Then how does rolling a dice 5,000 times work? What do you have to change?

 

Write this down as pseudo code or in plain English. When you know exactly what you need to do, then you can write the PHP code. Making random code changes and hoping that the script will at some point do what you want isn't a very effective strategy.

Link to comment
Share on other sites

I suggest you forget about PHP for now and just try to understand the basic logic. How does rolling a dice once work? What are the exact steps? Then how does rolling a dice 5,000 times work? What do you have to change?

 

Write this down as pseudo code or in plain English. When you know exactly what you need to do, then you can write the PHP code. Making random code changes and hoping that the script will at some point do what you want isn't a very effective strategy.

Thanks, but isn't the frequency I have on there rolling once? The "writing it down in pseudo code and plain English" isn't going to help me, I do better visually and not kinetically. I have the code as it what was explained in my class, the only issue is I do not know exactly where the 5000 goes (which I think it will go in the for loop) and why the random extra numbers are showing up in the left hand corner. 

 

Code is showing one roll!!!

Link to comment
Share on other sites

I tried putting it in my for loop and it didn't work. It gave me the same problem, but instead of only one number, it has given me a random 6 digit code underneath the title.

A for loop will work. If the code you wrote does not work and you want us to help then we need to see what you tried. 

 

Here is what the part of my assignment says: "Inside the PHP code block, create six variables to store the frequency of each side of the die. Choose your variable names wisely. For example, you may name them $frequency1, $frequency2 ……" so it doesn't start with 0.

Do you understand what those variables are supposed to mean? Being a programmer is not about following recipes. You have to actually know what you're doing. Think about what you're doing.

 

These variables count how many times each die face was rolled, right? So why does your code think 6 has been rolled six times before it has even executed?

 

The "writing it down in pseudo code and plain English" isn't going to help me, I do better visually and not kinetically.

If you think writing pseudocode is a kinetic exercise then you don't know what it means to write pseudocode.
Link to comment
Share on other sites

I actually had to go to the tutor at school, since I couldn't get any help. I had him explain it, since some of the code we never used or put together, like the rand($min, $max) with a for loop, so having me writing out the code in pseudocode wouldn't have worked! Also, I had the Switch conditional function having echos, which made a random code on top of my table, so that was alleviated. Thanks anyway, guys!

Link to comment
Share on other sites

Common sense and logical thinking have nothing to do with PHP. I'd say those are basic intellectual skills which every human should have.

I know I lack common sense, but when you are not taught something that is needed for your project and you really don't know how to do something and are asking for help, that is a different story. I went to the school tutor for help and he taught me how to do it by giving me an example and it was nothing like what was taught in class, so I just couldn't get it. I got everything in class til now, but I couldn't get this. Again, I am a student and I am still learning!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.