tsnickle Posted February 23, 2011 Share Posted February 23, 2011 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> Quote Link to comment Share on other sites More sharing options...
tsnickle Posted February 23, 2011 Author Share Posted February 23, 2011 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. Quote Link to comment Share on other sites More sharing options...
Alex Posted February 23, 2011 Share Posted February 23, 2011 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 Quote Link to comment Share on other sites More sharing options...
tsnickle Posted February 23, 2011 Author Share Posted February 23, 2011 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> Quote Link to comment Share on other sites More sharing options...
Alex Posted February 23, 2011 Share Posted February 23, 2011 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 23, 2011 Share Posted February 23, 2011 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. Quote Link to comment Share on other sites More sharing options...
tsnickle Posted February 23, 2011 Author Share Posted February 23, 2011 $results[1]++; $results[2]++; $results[3]++; $results[4]++; $results[5]++; $results[6]++; So is that how it should look? Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 23, 2011 Share Posted February 23, 2011 $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. 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.