Easily generate random data for games.
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 InitState. To learn how to save and restore the state of Random, see 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.Random
is a static class, and so its state is globally shared. Getting random numbers is easy, because there is no need to new
an instance and manage its storage. However, static state is problematic when working with threads or jobs (the generator will error if used outside the main thread), or if multiple independent random number generators are required. In those cases, managing instances of System.Random
would be a better option.
Float upper bounds are inclusive
All properties and methods in UnityEngine.Random
that work with or derive work from float-based randomness (for example value or 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, System.Random.NextDouble()
has an exclusive maximum, and will never return the maximum value, but only a number slightly below it.
Performance
Methods in UnityEngine.Random
have been measured to be between 20% and 40% faster than their equivalents in System.Random
.
Name resolution ambiguity
Because the classes share the name Random
, it can be easy to get a CS0104 "ambiguous reference" compiler error if the System
and UnityEngine
namespaces are both brought in via using
. To disambiguate, either use an alias using Random = UnityEngine.Random;
, fully-qualify the typename e.g. UnityEngine.Random.InitState(123);
, or eliminate the using System
and fully-qualify or alias types from that namespace instead.
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). |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.