What is the difference between System.Random and UnityEngine.Random?
What is the difference between System.Random and UnityEngine.Random? What should be preferred?System.Random or UnityEngine.Random?
Answer by tanoshimi · Mar 06, 2016 at 03:30 PM
Note that UnityEngine.Random is a static class that provides only a single, "global" stream of random values per game. If you want genuinely random values, it's fine.
However, each instance of System.Random you create is an independent random number generator stream. Each generator is only advanced when its nextValue() is called, which means it's much easier to create reproducible sequences of random numbers starting from a given seed.
Suppose you had two completely separate events in your game that both had a degree of random behaviour - the frequency of loot drops in a chest, and a variance in the speed with which bullets fired from a gun. If you use UnityEngine.Random, you're making those two random distributions entwined - the number of times you've fired your gun will influence the value that determines whether you get ultra-rare armour from a chest. This, in turn, makes testing your game very hard. So, for anything other than completely trivial random behaviour, I recommend using System.Random (and also initialising with a known seed)
:o
That answer made me rethink all my life choices!
Great example!
Good explanation, but in the end. Both takes the cpu clock cycle to return the "random" number right? Could someone explain why would influence ? For sure you will have distint values for each implementantion ( since they will use different seed), but if you make the test 1 million times, It should be the same results. Can anyone clarify if I am wrong and why?
You can still get the same behaviour with Unity's by caching the state of the random generator: var oldState = Random.state; Random.Range(-10.0f, 10.0f); Random.state = oldState; Random.Range(-10.0f, 10.0f);
. This will always generate the same two numbers.
Answer by Max_power1965 · May 04, 2020 at 10:48 AM
This is an old post, but if you have any doubt, I found a full a very detailed article that explains when to use one or another: https://gamedevelopertips.com/unityengine-random-vs-system-random/
Answer by Dante_CyberdeckGames · Mar 06, 2016 at 02:54 PM
Unity's Random is a utility for random values. It has additional features, such as returning a random rotation. See the documentation here: http://docs.unity3d.com/ScriptReference/Random.html
System.Random is for generating random values in a range.
It doesn't matter which one you use. For just getting a random number, I prefer System.Random since it is easy to use and part of the core system library.
Your answer
![](https://koobas.hobune.stream/wayback/20220612070801im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can you do borderlands style random weapons and stat generation? 2 Answers
Choose something randomly based on its grouping 0 Answers
Question about "shuffling" a deck of cards 2 Answers
I'm trying to shuffle an array's order 3 Answers
null array? 0 Answers