Jump to content
  • Advertisement
Sign in to follow this  
george7378

Finding the current average of an indefinitely increasing sequence

This topic is 1133 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone,
 
The problem I'm currently trying to solve is this: I have a function which generates a value each time it runs, and I want to keep repeating the function indefinitely. Each time the function returns a new value, I want to find the average of ALL of the values it has generated so far. How can I do this without storing every single value?
 
A bit of context: I'm trying to create a path tracer which will continue to cast rays indefinitely. Each iteration will cast one ray per pixel, and I want to display an average of ALL the rays cast so far, so that I can see the rendering happening in realtime. How can I create a blended image like this without storing every frame?
 
Thanks a lot, I feel like this is a simple mathematical concept which will be obvious when it is pointed out to me!

Share this post


Link to post
Share on other sites
Advertisement

You might find this Wiki article on the moving average useful. Specifically, the section on the cumulative moving average looks to me like what you're looking for, but there are discussions and derivations of other methods therein.

Share this post


Link to post
Share on other sites

Another really simple way to do it is to do

newAverage = prevAverage * (i - 1) / i + newValue / i;

, where i starts at 1 and increments after every time you want to add something to the running average. Granted, this is only as good as the floating point precision you're using, but then again, that could be said about almost everything :P

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!