Gaomon Posted February 10, 2017 Share Posted February 10, 2017 (edited) 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 February 10, 2017 by Gaomon Quote Link to comment Share on other sites More sharing options...
requinix Posted February 10, 2017 Share Posted February 10, 2017 $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? Quote Link to comment Share on other sites More sharing options...
Gaomon Posted February 10, 2017 Author Share Posted February 10, 2017 $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! Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted February 10, 2017 Share Posted February 10, 2017 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. Quote Link to comment Share on other sites More sharing options...
Gaomon Posted February 10, 2017 Author Share Posted February 10, 2017 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!!! Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted February 10, 2017 Share Posted February 10, 2017 “The only problem” is that you don't understand what the code even does and are now trying to approach the task with trial-and-error. This isn't going to work. Programming is not about luck. Quote Link to comment Share on other sites More sharing options...
requinix Posted February 10, 2017 Share Posted February 10, 2017 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. Quote Link to comment Share on other sites More sharing options...
Gaomon Posted February 10, 2017 Author Share Posted February 10, 2017 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! Quote Link to comment Share on other sites More sharing options...
Barand Posted February 10, 2017 Share Posted February 10, 2017 Here's your pseudocode intialize 6 variables to 0 loop 5000 times get N = random number between 1 and 6 increment variable N by 1 endloop output the 6 variables Quote Link to comment Share on other sites More sharing options...
Gaomon Posted February 10, 2017 Author Share Posted February 10, 2017 Nope, never learned how to do pseudocode, so it wouldn't have worked for me! It's all good. I got it worked out! Quote Link to comment Share on other sites More sharing options...
requinix Posted February 11, 2017 Share Posted February 11, 2017 And these are the people companies will hire to replace us 1 Quote Link to comment Share on other sites More sharing options...
Gaomon Posted February 12, 2017 Author Share Posted February 12, 2017 Um... I am only taking this class, because I am required to. I am a video major, not a coding major, so I am sorry I am new to PHP! Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted February 12, 2017 Share Posted February 12, 2017 Common sense and logical thinking have nothing to do with PHP. I'd say those are basic intellectual skills which every human should have. Quote Link to comment Share on other sites More sharing options...
Gaomon Posted February 12, 2017 Author Share Posted February 12, 2017 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.