+ Reply to Thread
Results 1 to 8 of 8

Thread: PHP / Maths problem

  1. #1
    Tom743's Avatar
    Tom743 is offline x10Hosting Member Tom743 is an unknown quantity at this point
    Join Date
    Dec 2007
    Posts
    72

    PHP / Maths problem

    Basically, the average for the number of rolls on my script isn't working. It keeps going up, until it gets higher than 6 which is impossible because the numbers generated are between 1 and 6. I will +rep anyone who can help me


    PHP Code:
    <?php
    $number 
    rand("1""6"); 
    $numberofroll file_get_contents("roll.txt") + 1
    $average file_get_contents("average.txt"); 
    $averagevote $average $number $numberofroll

    $file "roll.txt"
    $write fopen($file"w"); 
    fwrite($write$numberofroll); 
    fclose($write); 

    $file "average.txt"
    $write fopen($file"w"); 
    fwrite($write$averagevote); 
    fclose($write);

    echo 
    "You rolled {$number}. The average number is {$averagevote}";
    ?>
    EDIT: I forgot to say- avarage.txt has the current average roll in, and roll.txt has the number of rolls made in it.
    Last edited by Tom743; 09-30-2008 at 02:16 PM.

  2. #2
    TechAsh's Avatar
    TechAsh is offline Retired TechAsh is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    UK
    Posts
    5,853

    Re: PHP / Maths problem

    I'm not very good with PHP, but I think I've worked out what you are doing and therefore what you are doing wrong.

    You are taking the previous average from the file then adding the current roll to it, then dividing by the number of rolls.
    What I think you should be doing is keeping a list of what is rolled each time, then adding up all the numbers in that list and dividing by the number of rolls.

    Example:
    I think you are doing something like this:
    Previous rolls: 1,2,3,4
    Average: 2.5 (10 / 4)
    But if you store 2.5 in the file next time you take an average this would happen:
    Previous Average: 2.5
    Next Roll: 5
    Sum: 2.5 + 5 / 5 = 1.5
    This is wrong because:
    All Rolls: 1,2,3,4,5
    Average: 15 / 5 = 3

    Do you get what I mean?
    Last edited by TechAsh; 09-30-2008 at 02:23 PM.
    Useful Links:
    Terms of Service | Server News | Buy a Domain
    Free Domains: co.cc | Dot.tk -- Free File Storage: Dropbox -- Website Monitoring: Service Uptime


    My Websites:
    Earthtime Games & TechAsh's Blog

  3. #3
    Tom743's Avatar
    Tom743 is offline x10Hosting Member Tom743 is an unknown quantity at this point
    Join Date
    Dec 2007
    Posts
    72

    Re: PHP / Maths problem

    Quote Originally Posted by TechAsh View Post
    I'm not very good with PHP, but I think I've worked out what you are doing and therefore what you are doing wrong.

    You are taking the previous average from the file then adding the current roll to it, then dividing by the number of rolls.
    What I think you should be doing is keeping a list of what is rolled each time, then adding up all the numbers in that list and dividing by the number of rolls.

    Example:
    I think you are doing something like this:
    Previous rolls: 1,2,3,4
    Average: 2.5 (10 / 4)
    But if you store 2.5 in the file next time you take an average this would happen:
    Previous Average: 2.5
    Next Roll: 5
    Sum: 2.5 + 5 / 5 = 1.5
    This is wrong because:
    All Rolls: 1,2,3,4,5
    Average: 15 / 5 = 3

    Do you get what I mean?
    Yeah, I get what you mean, thanks I was thinking of doing it it like that but I thought it will take a long time to execute because there could be hundreds of numbers to add up and devide.

  4. #4
    TechAsh's Avatar
    TechAsh is offline Retired TechAsh is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    UK
    Posts
    5,853

    Re: PHP / Maths problem

    The problem is that you can't always cut corners in Maths.
    Useful Links:
    Terms of Service | Server News | Buy a Domain
    Free Domains: co.cc | Dot.tk -- Free File Storage: Dropbox -- Website Monitoring: Service Uptime


    My Websites:
    Earthtime Games & TechAsh's Blog

  5. #5
    cerbere is offline x10Hosting Member cerbere is an unknown quantity at this point
    Join Date
    Nov 2007
    Posts
    51

    Re: PHP / Maths problem

    Better do it this way if you don't want to keep the list of
    individual rolls in your file : use a weighted sum.

    Pseudo-
    Code:
    read current_avg, current_n_of_rolls, this_roll
    
    new_n_of_rolls = current_n_of_rolls + 1
    new_avg = (current_avg * current_n_of_rolls + this_roll) / new_n_of_rolls
    
    write new_avg, new_n_of_rolls
    The problem with your original script was that
    Code:
    $averagevote*=*$average*+*$number*/*$numberofroll;
    can only grow, blasting through 6.

  6. #6
    quantum1's Avatar
    quantum1 is offline x10Hosting Member quantum1 is an unknown quantity at this point
    Join Date
    Sep 2008
    Location
    near Nashville, TN
    Posts
    68

    Re: PHP / Maths problem

    I believe you want the files to store the number of rolls so far and the grand total of all rolls so far. You can compute the running average as the grand total of all rolls divided by the total number or rolls (values obtained from the files). You can present the average to the user when showing them their roll. You then add 1 to the total number of rolls and add their roll value to the grand total of all rolls, ready to be used the next time. Example shown below, columns may not line up but you get the idea:
    Roll # Rolls Total Average
    5 1 5 5
    3 2 8 4
    1 3 9 3
    6 4 15 3.25
    4 5 19 3.8

  7. #7
    mattura's Avatar
    mattura is offline x10 Elder mattura is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    563

    Re: PHP / Maths problem

    Yep, the weighted average suggested by cerbere and quantum1 is the way to cut the corner (cos sometimes you can).

    To clarify quantum1's example here is another with more symbols ;):
    imagine rolls 1,5,3,4,2... (average 3)
    each time, you count how many rolls have occurred, and get the number of the roll. Multiply the average by the number of rolls, add the new roll, then divide by the total plus one.
    Roll 1: (x*0 +1) /1 = 1 //x can be any number initially :P
    Roll 2: (1*1 +5) /2 = 3
    Roll 3: (3*2 +3) /3 = 3
    Roll 4: (3*3 +4) /4 = [13/4]
    Roll 5: ([13/4]*4 +2) /5 =3
    ...
    ----
    Life is a game. The conception is terrible but the graphics are amazing!
    matt.elementfx.com

  8. #8
    quantum1's Avatar
    quantum1 is offline x10Hosting Member quantum1 is an unknown quantity at this point
    Join Date
    Sep 2008
    Location
    near Nashville, TN
    Posts
    68

    Re: PHP / Maths problem

    Thanks for the question and everyone's involvement. This reminds me of some areas at work where I can use some running averages to compare systems, response times, etc. This is a cool forum.

+ Reply to Thread

Similar Threads

  1. currently have an application pending php
    By biomasti in forum Free Hosting
    Replies: 1
    Last Post: 09-03-2008, 01:58 PM
  2. Replies: 3
    Last Post: 03-10-2008, 12:22 PM
  3. problem in php mail function and smtp
    By idrees in forum Free Hosting
    Replies: 2
    Last Post: 02-08-2008, 05:16 PM
  4. "PHP Startup: Invalid Library" - Interesting error
    By javaguy78 in forum Free Hosting
    Replies: 5
    Last Post: 03-27-2007, 02:33 PM
  5. php ad code problem
    By sourabhj in forum Free Hosting
    Replies: 7
    Last Post: 08-22-2006, 08:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers