Click or drag to resize

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.

Note Note

An online version of this documentation is available here, and a downloadable archive for offline use can be acquired here.

What does Make It Random Basic offer?

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:

Note Note

The full version of Make It Random includes additional features, ranging from random colors and vectors to shuffling and list element selection. Full documentation can be found online here, and the library can be acquired on the Unity Asset Store.

How is Make It Random Basic organized?

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 UnityEngineRandom, 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.)

Note Note

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.

Using Make It Random

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.

Pulling in the Make It Random namespace.
using Experilous.MakeItRandom;

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.

Creating a standard random engine instance.
// 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().

Creating an explicitly typed random engine instance.
// 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:

Make It Random Usage 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);
See Also