🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

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

Started by
3 comments, last by Bregma 4 years, 10 months ago

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?

Advertisement

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:

image.png.4d98115c9a922e20b9e2f3bd3841cc31.png

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

 

 

 

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.

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.

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement