Jump to content

Dice Rolling Help


tsnickle

Recommended Posts

I need some help with a dice rolling project. Here is what code I've got so far I just need some help with the for and while loops. I have to simulate the the dice being rolled (between 1-6) 5000 times and track the results in a table. If anyone could help me, I'd be VERY APPRECIATIVE.  :-\

 

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title>"Statistical analysis of rolling a six-sided die"</title>

    </head>

    <body>

        <h2>Statistical analysis of rolling a six-sided die</h2>

        <br/>

        <table width='600' border='1' cellspacing='0' cellpadding='5' align='center'>

        <?php

            $side1 = 1;

            $side2 = 2;

            $side3 = 3;

            $side4 = 4;

            $side5 = 5;

            $side6 = 6;

 

            for ($i =1; $i <= 5000; i++) {

                $roll = mt_rand(1, 6);

               

 

            }

            ?>

 

        </table>

    </body>

</html>

 

Link to comment
https://forums.phpfreaks.com/topic/228627-dice-rolling-help/
Share on other sites

I'm doing a statistical analysis of rolling a six-sided die and I'm making a table. I am having some problems with the for and while loops. The output is suppose to show a table with the faces (1-6) and the frequency of each one, 5000 times. It needs to use rand($min, $max) to generate it and store it in $random ($random = rand($min, $max). If anyone could help me, I'd be very thankful. I just need a little push to get going with the loops and I think I can get the rest. Just kinda stuck.

Link to comment
https://forums.phpfreaks.com/topic/228627-dice-rolling-help/#findComment-1178792
Share on other sites

Why don't you post what you have so far?

 

If you just need help with the loops maybe the documentation and examples will suffice: for, while

 

Here is what I got so far.

 

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title>"Statistical analysis of rolling a six-sided die"</title>

    </head>

    <body>

        <h2>Statistical analysis of rolling a six-sided die</h2>

        <br/>

        <table width='600' border='1' cellspacing='0' cellpadding='5' align='center'>

        <?php

            $side1 = 1;

            $side2 = 2;

            $side3 = 3;

            $side4 = 4;

            $side5 = 5;

            $side6 = 6;

 

            for ($i =1; $i <= 5000; i++) {

                $roll = mt_rand(1, 6);

               

 

            }

            ?>

 

        </table>

    </body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/228627-dice-rolling-help/#findComment-1178797
Share on other sites

I realized that you created another thread with the code, so I merged them.

 

You already have the loop, so now just output a new table row for each loop.

 

To record the frequencies of each result you can create an array with an index for each result (1-6) and initialize each element with a value of 0. Then simply increment that element every time it's rolled.

Link to comment
https://forums.phpfreaks.com/topic/228627-dice-rolling-help/#findComment-1178800
Share on other sites

There is no reason to define $side1, $side2, etc. There is no purpose for them - as you have them now.

 

SO, you have a for() loop that runs 5,000 times and generates a random number between 1 and 6. So, based upon your statements I don't think you want to display the results of each roll. Instead you need to store the results of each roll then display a table showing the results.

 

So, your next step is to store the results of rand(). You could use $side1, $side2, etc. to store how many times each side is rolled. But, then you have to create multiple IF/ELSE statements or a switch. That's too much work. Just create an array called $results. Then use the index of the array for each possible result (i.e. $result[1] is for when a 1 is rolled). Increase the value for the appropriate index by 1 based the value of the roll.

$results[$roll ]++;

 

Then when you are complete with the for() loop you have an array something like this

array(
    1 => 785,
    2 => 924,
    3 => 1022,
    4 => 852,
    5 => 935,
    6 => 482
)

 

You can then calculate how often a particular number was rolled and display the results.

 

Link to comment
https://forums.phpfreaks.com/topic/228627-dice-rolling-help/#findComment-1178806
Share on other sites

$results[1]++;

            $results[2]++;

            $results[3]++;

            $results[4]++;

            $results[5]++;

            $results[6]++;

 

So is that how it should look?

 

On each iteratin of the loop, you would only increment the one matching the result of the roll.

Link to comment
https://forums.phpfreaks.com/topic/228627-dice-rolling-help/#findComment-1178850
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.