I’ve found that while using Random.Range for some things is useful, but there are other cases where just getting a random number is not all that you need.
An extremely common case is to need a random boolean. Most of us will just do if (Random.Range(0, 1) > 0.5f) doSomething(). This is a good mathematically random algorithm, however, humans are very bias and in a game, we should feed into those biases. If your weapon has a 10% chance of misfiring, you would never expect your weapon to ever misfire twice in a row, even though mathematically, it should happen every hundredth shot. This is why I wrote a script that takes a likelihood and prevents strings of low percentage values to be more in line with what people THINK 50/50 should look like.
Another common case that raw random values can’t handle properly is the type of situation where you want a random value centered around another value. For example, if you want your shotgun to do between 10 and 20 points of damage, you don’t want each shot to just be some value between 10 and 20, you want MOST of your shots to be around 15. The bell curve random class is what I created for that case. It’ll much more heavily weight the values in the middle of the range than those towards either bound.
private void example(){
RNJesus.seed((int)(new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()));
string evenIntsZeroToTen = "";
string evenFloatsZeroToOne = "";
string bellCurveIntsZeroToTen = "";
string coinFlips = "";
for(int i = 0; i < 20; ++i){
evenIntsZeroToTen += RNJesus.even().rangeInclusive_i(0, 10) + ", ";
evenFloatsZeroToOne += RNJesus.even().rangeInclusive_f(0, 1) + ", ";
bellCurveIntsZeroToTen += RNJesus.bell().rangeInclusive_i(0, 10) + ", ";
coinFlips += RNJesus.boolean().boolean(0.5) ? "H, " : "T, ";
}
Debug.Log(" Even Distribution 0 to 10: " + evenIntsZeroToTen);
Debug.Log(" Even Distribution 0 to 1: " + evenFloatsZeroToOne);
Debug.Log(" Fifty/fifty shot: " + coinFlips);
Debug.Log(" Bell Curve Distribution 0 to 10: " + bellCurveIntsZeroToTen);
}
In this folder you will find the RNJesus class as well an a MonoBehavior component that contains unit tests for it.
5375118–544206–RNJesus.zip (6.15 KB)