Welcome to the Make It Random Basic User's Manual. This document will assist in integrating this collection of random number generation utilities into your Unity project.
This topic contains the following sections:
Make It Random Basic is a fast, flexible, and extensible random number generation library for Unity. It provides classes and functions, accessible from scripts, for generating random data of various sorts, including:
The foundation of Make It Random Basic is the concept of a Random Engine, an object providing the basic services of a pseudo-random number generator (PRGN), responsible for generating a deterministic sequence of seemingly random bits initialized by a seed value. Unlike , Make It Random engines are standard non-static classes which can be passed around, copied, and serialized. Multiple instances can exist simultaneously and independently, making it easier to control deterministic behavior when it is needed. (A static global instance does exist for convenience, however.)
On top of random engines, extension methods are available for using the random bits they generate to produce all of the more structured and complex data typically required by games. Because they are extension methods which apply to the Experilous.MakeItRandomIRandom interface which all Make It Random engines implement, it is easy to add further extension methods that will immediately become usable by any random engine type. Similarly, if for some reason the random engine supplied by Make It Random Basic is not suitable and you need to create your own, just make sure it implements IRandom and your new engine will automatically support all of the already existing utilities. (See Making Your Own Random Engine for more information.)
The Make It Random library is located within the Experilous.MakeItRandom namespace. Be sure to put the appropriate using directive at the top of your scripts, or explicitly qualify type names within your code.
The first step in using Make It Random is to pull in the namespace Experilous.MakeItRandom near the top of any script file in which you plan to use it. Without doing so, none of the extension functions for IRandom will not be available, making usage syntax far more verbose.
The next step is to create an instance of a random engine, probably within an Awake() or Start() function. The easiest way to do that is by using MIRandom.CreateStandard(). This selects a random engine type that is suitable for general use and constructs an instance of that type. If you pass no parameters, the engine will be set to a reasonably unpredictable state. You may instead pass a seed, such as an int or a string, to generate the same sequence each time the program is executed.
// Create a random engine with an unpredictable state. var random = MIRandom.CreateStandard();
If you know which random engine type you wish to use (XorShift128Plus is the only one included in the basic edition), then you may use a Create() function on that type to do so, just as with MIRandom.CreateStandard().
// Create a random engine, initializing its state with a string seed. var random = XorShift128Plus.Create("My random seed!!1!11!~!");
Using that created instance, you can now call any of the numerous extension methods, depending on your needs. Here are some examples:
// Generate an integer where 0 <= n < 144 int num1to10 = random.RangeCO(144); // Generate an integer where 1 <= n <= 10 int num1to10 = random.RangeCC(1, 10); // Generate a number where 0 <= n < 1 float num0to1 = random.FloatCO(); // Generate a number where -1 < n < +1, with high precision near 0 float num0to1 = random.PreciseSignedFloatOO(); // Get 52 random bits ulong bits = random.Bits64(52); // Flip a coin bool heads = random.Chance(); // Flip an unfair coin bool unfairHeads = random.Chance(51, 49); // Check a 3 in 10 probability bool criticalHit = random.Probability(3, 10);