PHP - Dice Rolling Help
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> Similar TutorialsI had a question and couldn't really pin-point a forum to put this post in, so I hope this suits my topic. You always hear "Facebook is rolling out a new feature"; most recently "Twitter is rolling out the New Twitter.com" but it only releases to so many people at time. Or friends will get features on Facebook before I do (like I couldn't like threaded comments until about a month after all my other friends could). I was just wondering if anyone knows the actual method or how large sites go about releasing new features to users? So i am a noob with php, I am looking to design a ten sided dice roller that shows the results to everyone along with who rolled it. Hello everyone,
<?php $sql = ";WITH TOTAL_KWH_WINTER AS ( SELECT CONVERT(VARCHAR(10),cdate,111)AS trans_date, datepart(hh, cdate) as trans_hour, comm_id, MIN((total_energy_a+total_energy_b+total_energy_c)/100) AS minUsage, MAX((total_energy_a+total_energy_b+total_energy_c)/100) AS maxUsage, meter_multiplier FROM [radiogates].[dbo].[purge_data] LEFT OUTER JOIN [radiogates].[dbo].[ops_invoice] on [radiogates].[dbo].[purge_data].[comm_id] = [radiogates].[dbo].[ops_invoice].[meter_id] where comm_id='$comm_id'and meter_multiplier is not null group by comm_id, CONVERT(VARCHAR(10),cdate,111), datepart(hh, cdate), meter_multiplier ) SELECT *, datepart(weekday, trans_date) as trans_date_day, datepart(month, trans_date) as trans_date_month, ((maxUsage - minUsage)*meter_multiplier) as totalUsage FROM TOTAL_KWH_WINTER where datepart(weekday, trans_date) IN ('1', '2', '3', '4', '5', '6', '7') AND DATEPART(MONTH, trans_date) IN ('10','11','12','1','2','3','4') and trans_date BETWEEN '$startdate2 00:00:01' AND '$enddate2 24:00:00' "; $query = sqlsrv_query($conn, $sql);if ($query === false){ exit("<pre>".print_r(sqlsrv_errors(), true));}while ($row = sqlsrv_fetch_array($query)){ $sumUsageKWH += $row[totalUsage];}sqlsrv_free_stmt($query);?>**************For whatever reason, my browser will not let me put this code where it needs to go, so its up top. Sorry for the inconvenience.************* My concern are code lines 4 through 15 I have PHP code that queryies MSSQL for data called kilowatt hours (kWh), set in a combination of three columns. kWh is set into the db table once every minute and it a value that starts at zero (0) at the beginning of the meters life, then continues until it reaches 10,000,000 before resetting back to zero (0). This happens over weeks, months or years, but it does happen. The code I have listed here makes no consideration of this reset. Since we are alsways querying between a start date and a stop date, we need to know the kWh usage between that period. So, we used maxUsage - minUsage and called that the totalUsage. That works great until the data reset at 10M. At that point, max usage will be some value (ex. 1 kWh), where min usage will be some value (ex. 9,999,999 kWh). the system breaks down at that point. I need to find an algorithm that helps me take into consideration this rolling number set. Thanks for any help! Edited by Butterbean, 16 January 2015 - 09:28 AM. I am writing a stock program and I am going to be getting several averages on volume over different periods, 10, 21, 50,100,120,150,200,240 are my averages.
I would like to make this fairly efficient, seeing as how its going to be a ton of data, I have over 7700 stocks in the database and they will each have all of those averages.
My question is how to do this that is the most efficient, I thought about taking the 10 day then adding 11 to it, then adding 29 to get the 50 day and so on and so forth, or is that too much work and I should just do each average individually?
select avg(volume) from _AAPL where date between ‘2014-02-03’ and ‘2014-05-05’;The above is my current very generic code that does work, now I just need to make a php script, which I can do, I am just curious about how different people would approach this. Thanks for the help. Edited by illcrx, 23 September 2014 - 10:32 PM. Hey guys. I'm new around here, and I come to you to get some help on a coding. I have made a program that selects two variable via the POST function and generates a set amount of random numbers between 1 and $B. That thing works like a charm and it places them all neatly in a table, but now I want to get on with the program and make it count the amount of all the numbers specified in variable $C. EXAMPLE: Generated 9 random numbers with a value between 1 and 10, echoing out the following: 9 8 2 6 4 7 3 10 5 Now I want it to count the numbers 8 and 9, telling me how many there are. If I can get this to work, I then want it to count the amount of 10's, add them to the amount of 8's and 9's, plus reroll them, add the following 8's and 9's, then repeat the process of counting and rerolling the 10's, then adding the 8's and 9's. Any of you have an idea of how to make this work? I am not a very good PHP programmer, and I got a project to "work" on. I need help writing a Dice Roll Function. This is how I'm supposed to do the two functions: New Function: dice_roll (int sides) - $ returns a random number of how many sided the dice is, between 1 and the $. New Function: dice_roll_str (string dicestr) - A string describing the dice [0-9] + d [0-9 ]+[+-][ 0-9] + size (d <number_of_dices> <number_of_sides> [+-][modifying_value]) - For example if I call the function with "3d6-3", this means: we throw 3 times with a 6-sided dice, add it up, then subtract 3-t. - The function is to return a false value on error - Possible errors: - Minimum number of of sides of the dice is 2 the maximum is 24 - A minimum number of dices 1 the maximum is 8 - The result should always be a positive number, which is not greater than twice of the result we got. I've already done the first function. This is my code: Code: [Select] function dice_roll($sides_num) { $rand_num = rand(1, $sides_num); return $rand_num; } I'm not sure it correct. It is the second function that I don't know how to do. Please, if you can help I'd be really grateful. If you can please put explanations on how the code works. Thank you so much, Evermore Im new to php and i cant get my program to display correctly. I want it to take a user's guess of a single Die roll and display whether the user guessed correctly and display a picture of the corresponding Die. i think there is something wrong with my "If" statement or logic. Please Help. Here's the Code: INPUT FORM: <html> <body> <form name = "Dice Game" action="diceroll.php" method="post"> Your Guess: <br> <input type= "text" name="" size= "1"> <input type ="submit" name = "submit"> </form> </body> </html> OUTPUT FORM: <html> <body> <?php $roll = $_POST['submit']; $dice = rand(1, 6); if ($roll == 1 && $dice == 1){ print "Great Job, You're Good!"; }else if ($roll == 1 && $dice /= 1) { print "Wrong"; } else if ($roll == 2 && $dice == 2) { print "Great Job, You're Good!";} else if ($roll == 2 && $dice /= 2) { print "Wrong"; } else if ($roll == 3 && $dice == 3) { print "Great Job, You're Good!"; } else if ($roll == 3 && $dice /= 3) { print "Wrong"; } else if ($roll == 4 && $dice == 4) { print "Great Job, You're Good!"; } else if ($roll == 4 && $dice /= 4) { print "Wrong"; } else if ($roll == 5 && $dice == 5) { print "Great Job, You're Good!"; } else if ($roll == 5 && $dice /= 5) { print "Wrong"; } else if ($roll == 6 && $dice == 6) { print "Great Job, You're Good!"; } else if ($roll == 6 && $dice /= 6) { print "Wrong"; } else { print " "; } if($dice == 1) print "<img src='dice1.png'>"."<br>"; if($dice == 2) print "<img src='dice2.png'>"."<br>"; if($dice == 3) print "<img src='dice3.png'>"."<br>"; if($dice == 4) print "<img src='dice4.png'>"."<br>"; if($dice == 5) print "<img src='dice5.png'>"."<br>"; if($dice == 6) print "<img src='dice6.png'>"."<br>"; else { print "Thanks for playing". "<br>"; } ?> <form name= "back" action= "dice.php" method= "post"> <input type="submit" name="back" value ="Play Again"> </form> </body> </html> Hello, I have a simple dice system script I am using in a chat program for a website and I was wondering if someone could simply tell me how to limit the number of dice being rolled. Here are the two chunks of script that make it work: if($irc_cmd == '/d') { if($irc_cmd == $txt) { // this can only happen with a no dice - '/d' - a help request // return an explanation of the correct format $text = "dice command is /d [[n]D]s[Xm|+a|-d|/q]*"; $this->sendToUser(null, new Message($type, $this->userid, null, $text, $this->color)); return 'ok'; } // create standard failure message $invalid_msg = new Message($type, $this->userid, null, 'ERROR: invalid dice command - '.$txt.' - enter /d for help', $this->color); // remove the original command from the dice string $dicestring = str_replace($irc_cmd,'', $txt); // use lowercase versions of D and X to make parsing simpler $dicestring = strtolower($dicestring); // remove any user entered spaces $dicestring = str_replace(' ', '', $dicestring); // note that all modifiers will follow the dice spec 'nDs' // number of dice is optional e.g. 1d4 d4 and 4 all represent a single 4-sided die // if the first token is blank, then the user intended for one die e.g. /d d20 means /d 1d20 // if the is no 'd' then the user intended one die e.g. /d 20 means /d 1d20 $parts = explode('d', $dicestring); if(count($parts)>2) { // only one 'd' is allowed $this->sendToUser(null, $invalid_msg); return 'ok'; } elseif(count($parts)==1) { // if no 'D' assume '1' die $number = 1; } else { $number = (int)$parts[0]; if ($parts[0] == "") { // if $number == 0 // if no number assume '1' die $number=1; } elseif ("$number" != "$parts[0]") { // only integers allowed $this->sendToUser(null, $invalid_msg); return 'ok'; } $dicestring = $parts[1]; } if($number < 1) { // can't allow a negative number of dice $this->sendToUser(null, $invalid_msg); return 'ok'; } // check for sides and modifiers // expand the string to put spaces around all the tokens we want $dicestring = str_replace('+', ' + ', $dicestring); $dicestring = str_replace('-', ' - ', $dicestring); $dicestring = str_replace('x', ' x ', $dicestring); $dicestring = str_replace('/', ' / ', $dicestring); // explode the whole thing to create tokens $parts = explode(' ', $dicestring); // the only other tokens should be integers // allowed formats from here are s[Xm][+a][-d][/q] // we should allow any series of these in any order, applying them left to right // the first part must be the sides $sides = (int)$parts[0]; if ("$sides" != "$parts[0]") { $this->sendToUser(null, $invalid_msg); return 'ok'; } if($sides < 1) { // can't allow a negative number of sides $this->sendToUser(null, $invalid_msg); return 'ok'; } // get the user's name //$user = ChatServer::getUser($this->userid); //$name= $user['login']; // start writing the reply string $text = '*rolls* '.$number.'d'.$sides.': '; // seed the randomizer srand(time()); // with number and sides, roll the dice, adding them up $total = 0; for($i = 0; $i < $number; $i++) { $roll = (rand()%$sides)+1; if($i != 0) $text .= '+'; $text .= $roll; $total += $roll; } // now apply all the modifiers to the roll, in the order the user specified them for ($i = 1; $i < count($parts); $i+=2) { // the value needs to be an integer $value = (int)$parts[$i+1]; $v = $parts[$i+1]; if ("$value" != "$v") { $this->sendToUser(null, $invalid_msg); return 'ok'; } // the token needs to be one of the operators - otherwise abort $token = $parts[$i]; switch ($token) { case '+': // add $total += $value; break; case '-': // subtract $total -= $value; // make minimum 1 - remove this like to allow 0 and lower if ($total<1) $total=1; break; case 'x': // multiply $total *= $value; break; case '/': // divide - round up so 1d6/3 will be the same as 1d2 $total = ceil($total/$value); break; default: $this->sendToUser(null, $invalid_msg); return 'ok'; } // add the modifier to the display string $text .= $token.$value; } // and display the final result $text .= ': '.$total; // gets sent to particular room, but with users name tacked on, so a user could spoof it // at least 'msgu' looks different $this->sendToRoom(null, new Message('rpg', $this->userid, $this->roomid, $text)); return 'ok'; } and function Message($command, $userid = null, $roomid = null, $txt = null, $color = null) { $this->command = $command; if ($command == 'rpg') $this->command = 'msgu'; $this->userid = $userid; $this->roomid = $roomid; $this->color = htmlColor($color); if($command != 'rpg') { $txt = str_replace('*rolls*', 'rolls', $txt); } if(isset($txt)) { $this->txt = $this->parse($txt); } } So again all I want it to do is limit the number of dice being rolled because right now someone can do a /d 1000000d100000 and completely crash the chat for everyone. I am thinking 100 on either variable would be plenty. Thanks for your help! |