[Algorithm] Normal random in ACS

Maps, modifications, add-ons, projects, and other releases for Zandronum. Also includes announcers.
Post Reply
User avatar
Dusk
Developer
Posts: 581
Joined: Thu May 24, 2012 9:59 pm
Location: Turku

[Algorithm] Normal random in ACS

#1

Post by Dusk » Sat Dec 05, 2015 2:01 am

This is an algorithm for calculating random variables that approximately conform to the normal distribution. That is to say you can make bell curves with it in ACS. The library consists of two functions:

NormalVariate(fixed mean, fixed deviance)
Calculates a bell-curved random variable. This is probably what you want to use here. mean specifies the peak of the curve (i.e. the most common value) while deviance specifies how random the result is.

At 90% chance the result of NormalVariate() will be at most probit(0.95) * deviance = 1.644 * deviance away from the mean. That is, a NormalVariate(5.0, 2.5) will, at 95% chance return a number between [0.887, 9.112]. Respectively, at 75% chance the result will be at most probit(0.875) * deviance = 1.15 * deviance away from the mean.

Probit(fixed x)
Quickly calculates the x-quantile of the standard normal distribution. You probably won't be very interested in this one but it's the heart and soul of NormalVariate(). Uses two large lookup tables and a single iteration of Newton's method to find an approximate result efficiently.

Note that due to the corner cutting used in the implementation of Probit(x) the bell curve is an approximation. If x is not a multiple of 0.005 the result will be slightly off and if x is less than 0.0025 or greater than 0.9975 the result is flatly clamped to ±2.8 because the algorithm simply cannot handle such values.

This means that NormalVariate() will never provide values that are any further away from mean than 2.8 * deviance, even though a precise bell curve would yield such results in rare cases! Though I guess if you're using it for a mod the precision provided should be OK enough. However, also due to the corner cutting it's efficient enough to calculate 5,000 normal random values (20,000 in Zandronum 3.x and ZDoom) without hitting the runaway limit.

You may use it freely in mods but give credit for its use or I shall find you down and stuff something really normally random down your neck.

Download (v1)
Last edited by Dusk on Sat Dec 05, 2015 2:04 am, edited 1 time in total.

User avatar
Dusk
Developer
Posts: 581
Joined: Thu May 24, 2012 9:59 pm
Location: Turku

RE: [Algorithm] Normal random in ACS

#2

Post by Dusk » Thu Dec 24, 2015 12:28 am

Essentially it provides an alternative way of computing random values, where the value will most of the time revolve around the given mean while still allowing for rare cases to go into the extremes.

It's like throwing two dice where the total will most of the time be around 7 and can rarely go towards 2 or 12. Rolling three will bias the total towards the average (10.5) much more strongly than rolling two and the effect goes up from there. The normalvariate() function lets you specify the average and the strength of the bias so it's like this effect generalized; compare Random() which is like rolling a single die.
Last edited by Dusk on Thu Dec 24, 2015 12:29 am, edited 1 time in total.

User avatar
Ivan
Addicted to Zandronum
Posts: 2229
Joined: Mon Jun 04, 2012 5:38 pm
Location: Omnipresent

RE: [Algorithm] Normal random in ACS

#3

Post by Ivan » Thu Dec 24, 2015 6:25 am

Looks nice, I'll check it out when I have time.

Is there are reason this wasn't planned to be added to Zandronum? Or is it planned?
=== RAGNAROK DM ON ... uh... dead forever? ===
=== ALWAYS BET ON ... uh... dead forever? ===
=== Who wanta sum wang? ===
=== Death and Decay - A new Monster/Weapon replacer ===

User avatar
Dusk
Developer
Posts: 581
Joined: Thu May 24, 2012 9:59 pm
Location: Turku

RE: [Algorithm] Normal random in ACS

#4

Post by Dusk » Thu Dec 24, 2015 11:39 am

I think it's a little too niche to be an engine feature, though I do have a C/C++ implementation at hand (which came first, this is a translation of it anyway). However I cannot post that until new years.

Post Reply