Controlling randomness to get a logarithmic result...

Hello, I need to control the result of Random.value using a logarithmic equation (and eventually other equations). I’ve been researching logarithms, and even though the desired result is a logarithmic curve, the way to get there isn’t clear at all. How would I apply a simple graphic equation to a random value?

I am currently issuing an if statement that looks like so:

if(Random.value > (varyingVariable/100)){
doStuff;
}

My variable is between 0 and 100. This might be wrong, but if the lower values come out more often, then it should work… I think :slight_smile:

I need to modify either the random value, or my variable, to get a lower number to return true more often than a higher number. Anyone can help? Am I missing some math knowledge here (most probably)?

Thanks a ton!

PS, I am working on a true to life simulation, and will eventually need to adapt this randomness to other equations. Something modular would be great. I will also be attending some much needed classes on maths next semester (college), so hopefully I might be able to answer myself… fingers crossed. Thanks for any help.

Probably the simplest way is to just use Random.value * Random.value, instead of Random.value.

If you really need a certain distribution, you can figure the domain, roll a uniform random (what Random.value gives you) in there, then apply your function. Say you want 0 to 5 in a log-2 distribution. 2^0 is 1 and 2^5 is 32, so you roll 1-32 and log it:

float res = Random.value*31+1; // 1-32
res = Mathf.log(res, 2); // convert to 0-5 with log distribution
                         // (if we roll 16-32, res is 4-5)

Of course, for real, log is high more often. Could flip it (use 6-res) or use exponenentiation (opposite of log) instead. 2^0 is still 1, and 2^2.2361 is 5, so:

float res = Random.value*2.2361; // 0-2.2361
res = Mathf.Pow(2, res); // convert to 0-5. If we roll 2-2.2361, we get 4-5