Random
Easily generate random data for games.
Read time 3 minutesLast updated 4 days ago
Definition
- Type: Class
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
public static class Random
Remarks
This static class provides several easy game-oriented ways of generating pseudorandom numbers.
The generator is an Xorshift 128 algorithm, based on the paper Xorshift RNGs by George Marsaglia. It is statically initialized with a high-entropy seed from the operating system, and stored in native memory where it will survive domain reloads. This means that the generator is seeded exactly once on process start, and after that is left entirely under script control.
For more details on the seed, including how to manage it yourself, see Random.InitState. To learn how to save and restore the state of Random, see Random.state.
Versus System.Random
This class has the same name as the .NET Framework class System.Random and serves a similar purpose, but differs in some key ways:
Static vs instanced
UnityEngine.RandomnewSystem.RandomFloat upper bounds are inclusive
All properties and methods in that work with or derive work from float-based randomness (for example Random.value or Random.ColorHSV) will use an inclusive upper bound. This means that it is possible, though as rare as any other given value, for the max to be randomly returned. In contrast, has an exclusive maximum, and will never return the maximum value, but only a number slightly below it.
UnityEngine.RandomSystem.Random.NextDouble()Performance
Methods in have been measured to be between 20% and 40% faster than their equivalents in .
UnityEngine.RandomSystem.RandomName resolution ambiguity
Because the classes share the name , it can be easy to get a CS0104 "ambiguous reference" compiler error if the and namespaces are both brought in via . To disambiguate, either use an alias , fully-qualify the typename e.g. , or eliminate the and fully-qualify or alias types from that namespace instead.
RandomSystemUnityEngineusingusing Random = UnityEngine.Random;UnityEngine.Random.InitState(123);using SystemStatic Properties
Property | Description |
|---|---|
| insideUnitCircle | Returns a random point inside or on a circle with radius 1.0 (Read Only). |
| insideUnitSphere | Returns a random point inside or on a sphere with radius 1.0 (Read Only). |
| onUnitSphere | Returns a random point on the surface of a sphere with radius 1.0 (Read Only). |
| rotation | Returns a random rotation (Read Only). |
| rotationUniform | Returns a random rotation with uniform distribution (Read Only). |
| state | Gets or sets the full internal state of the random number generator. |
| value | Returns a random float within [0.0..1.0] (range is inclusive) (Read Only). |
Static Methods
Method | Description |
|---|---|
| ColorHSV | Generates a random color from HSV and alpha ranges. |
| InitState | Initializes the random number generator state with a seed. |
| Range | Return a random |
Structs
Struct | Description |
|---|---|
| Random.State | Serializable structure used to hold the full internal state of the random number generator. Additional Resources: Random.state. |