x10Hosting Forums

Corporate Free Hosting for the Masses.



Register

Reply
 
LinkBack Thread Tools Display Modes
x10Hosting Member

Tom743's Avatar

Join Date: Dec 2007
Posts: 72
Credits: 760
Tom743 is on a distinguished road
Quote  
09-30-2008, 03:02 PM
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 03:16 PM.
Reply With Quote
Tom743 is offlineReport Post
Senior Account Manager

TechAsh's Avatar

Join Date: Oct 2007
Posts: 5,235
Credits: 22,299
TechAsh has a reputation beyond repute
Location: UK

Quote  
09-30-2008, 03:22 PM
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?
__________________
My Website - Earthtime Games is worth

Last edited by TechAsh; 09-30-2008 at 03:23 PM.
Reply With Quote
TechAsh is offlineReport Post
x10Hosting Member

Tom743's Avatar

Join Date: Dec 2007
Posts: 72
Credits: 760
Tom743 is on a distinguished road
Quote  
09-30-2008, 03:30 PM
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.
Reply With Quote
Tom743 is offlineReport Post
Senior Account Manager

TechAsh's Avatar

Join Date: Oct 2007
Posts: 5,235
Credits: 22,299
TechAsh has a reputation beyond repute
Location: UK

Quote  
09-30-2008, 03:37 PM
Re: PHP / Maths problem

The problem is that you can't always cut corners in Maths.
__________________
My Website - Earthtime Games is worth
Reply With Quote
TechAsh is offlineReport Post
x10Hosting Member

Join Date: Nov 2007
Posts: 24
Credits: 658
cerbere is on a distinguished road
Quote  
10-01-2008, 02:00 AM
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.
Reply With Quote
cerbere is offlineReport Post
x10Hosting Member

quantum1's Avatar

Join Date: Sep 2008
Posts: 33
Credits: 445
quantum1 is on a distinguished road
Location: near Nashville, TN

Quote  
10-01-2008, 11:16 PM
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
Reply With Quote
quantum1 is offlineReport Post
x10 Lieutenant

Join Date: Oct 2007
Posts: 438
Credits: 4,200
mattura is just really nice
Quote  
10-03-2008, 04:10 AM
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
...
__________________
----
Please donate credits if you had a really good reply from me! Rep is also appreciated. Thanks
matt.elementfx.com
Reply With Quote
mattura is offlineReport Post
x10Hosting Member

quantum1's Avatar

Join Date: Sep 2008
Posts: 33
Credits: 445
quantum1 is on a distinguished road
Location: near Nashville, TN

Quote  
10-07-2008, 11:15 AM
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 With Quote
quantum1 is offlineReport Post
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
currently have an application pending php biomasti Free Hosting 1 09-03-2008 02:58 PM
Problem compiling PHP zip extension onto a Mac that has MAMP Pro DarkAngelBGE Free Hosting 3 03-10-2008 01:22 PM
problem in php mail function and smtp idrees Free Hosting 2 02-08-2008 06:16 PM
"PHP Startup: Invalid Library" - Interesting error javaguy78 Free Hosting 5 03-27-2007 03:33 PM
php ad code problem sourabhj Free Hosting 7 08-22-2006 09:28 AM


All times are GMT -5. The time now is 07:11 PM. Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0 RC7
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

Cheap Loan | Mortgages | Loans | Debt Consolidation | Loans