Skew a pseudo-random number generator to usually return numbers in the middle of the given range?

Recommended Posts

I have a class for the NPCs in my game. Each NPC has an athleticism attribute that ranges from zero to one-thousand. I am randomly generating this value. I want 70%-80% people to have a roughly average amount of athleticism, somewhere close to 500.

Is there some algorithm I can apply that will skew the randomly determined athleticism score so that it's usually close to 500, but always returns a few scores that are either much lower or a lot higher?

Share on other sites

Make the random number in the range [0,1].

Map it to the distribution using a function like smoothstep.

Scale by your max value of 1000.

Instead smoothstep you could use something that allowas precise tuning, like this gain function:

	float gain(float x, float k) { const float a = 0.5*pow(2.0*((x<0.5)?x:1.0-x), k); return (x<0.5)?a:1.0-a; }


Taken from here: https://www.iquilezles.org/www/articles/functions/functions.htm

x is the random number, and k sets how steep the curve is.

Using 0.5 for k looks looks this:

The smaller k, the closer the acerage will be to the 0.5 you want.

Share on other sites

You can also just average a few random numbers between 0 and 1000. The more numbers you average, the more concentrated the distribution will be around 500. Play with it.

Share on other sites

You're talking about a non-uniform probability density function (PFD).  Typically you'd want a Gaussian, sometimes called Normal, distribution (the classic single-humped camel graph) which can be generated using something the like Box-Muller algorithm.

If you use the the right search terms you can find several libraries that will give you what you're looking for.

Create an account

Register a new account

• Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 12
• 14
• 10
• 33
• 23