Jump to content
  • Advertisement
Sign in to follow this  
RidiculousName

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 this post


Link to post
Share on other sites
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.

 

 

 

Share this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
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!