sum of multiple FRand()

Discussions about Coding and Scripting
Post Reply
User avatar
Barbie
Godlike
Posts: 2808
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

sum of multiple FRand()

Post by Barbie »

Is FRand() + FRand() + FRand() + FRand() statistically the same as 4 * FRand()?
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett
Buggie
Godlike
Posts: 2749
Joined: Sat Mar 21, 2020 5:32 am

Re: sum of multiple FRand()

Post by Buggie »

No, it is not.
4 random value is not same as one value increased in 4 times.

One value increase in 4 times keep all parameters increased in 4 times.

Sum of 4 value use different params.

For illustrate this, let's replace 4 with some big number like 100 000.

Sum of 100 000 random values very likely be 100 000 times of median for this random value.
When 100 000 multiplied on single random value still be random value, uniformly distributed in some range.

OFC for smaller N it less noticeable and for N = 1 effect is gone.
User avatar
Barbie
Godlike
Posts: 2808
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

Re: sum of multiple FRand()

Post by Barbie »

Thanks. I stumbled over that in class ThingFactory:

Code: Select all

else //timeDistribution is gaussian
	nextTime = 0.5 * (FRand() + FRand() + FRand() + FRand()) * interval;
what tries to approximate exp(-x²).
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett
Buggie
Godlike
Posts: 2749
Joined: Sat Mar 21, 2020 5:32 am

Re: sum of multiple FRand()

Post by Buggie »

It is Irwin-Hall distribution:

https://math.stackexchange.com/question ... -variables

https://en.wikipedia.org/wiki/Irwin%E2% ... stribution   
Auto merged new post submitted 9 minutes later
If they really need use gaussian distribution from uniform, they should use Box-Muller transform:
https://en.wikipedia.org/wiki/Box%E2%80 ... _transform   
Auto merged new post submitted 1 minute later
There you can more practical advice's about how do that:
https://stackoverflow.com/questions/756 ... stribution
User avatar
_21
Average
Posts: 70
Joined: Mon Aug 30, 2021 10:51 am

Re: sum of multiple FRand()

Post by _21 »

At N=4 the distribution is quite similar to a Gaussian, for a video game its a practical technique.
Buggie
Godlike
Posts: 2749
Joined: Sat Mar 21, 2020 5:32 am

Re: sum of multiple FRand()

Post by Buggie »

I found this for 3:

https://www.researchgate.net/figure/Irw ... _319887464
Irwin-Hall-distribution-with-n3-and-the-matching-normal-distribution-with-mean-3-2-and.jpg
Irwin-Hall distribution with n=3 and the matching normal distribution with mean 3/2 and variance 1/4.
   
Auto merged new post submitted 6 minutes later
According to wiki - best use sum of 12 values minus 6:
https://en.wikipedia.org/wiki/Irwin%E2% ... stribution   
Auto merged new post submitted 1 hour 18 minutes later
Also original formula is more like as Bates ditrubution:
https://en.wikipedia.org/wiki/Bates_distribution

(X + X + X + X) / 4 * (2 * Interval).
Post Reply