- Home /
Random.seed on different hardware?
Does anyone know if this method will present the same random numbers across different devices? Say both iOS and Android?
https://docs.unity3d.com/530/Documentation/ScriptReference/Random-seed.html
I know that seeds generate pseudo-random repeatable numbers. But I need it to do that across different specs of hardware. In most cases, the processor difference will present different sets of numbers for the same seed.
Answer by tanoshimi · Nov 21, 2016 at 09:59 PM
Given the same seed, Random.value will produce the same values in the same order whatever the target platform.
I wouldn't say that with too much certainty ^^. Depending on the reliabiliby that is needed It's generally not recommended to use it for repeatable sequences as Unity could replace the PRNG in the future.
Some related posts:
https://blogs.unity3d.com/2015/01/07/a-primer-on-repeatable-random-numbers/
http://answers.unity3d.com/questions/393825/systemrandom-with-seed-not-matching-net-or-mono.html
As far as i remember Unity once switched to a different PRNG in the past. Also i thought i read somewhere that someone had already problems with different platforms, especially WindowsPhone since it uses $$anonymous$$S .NET ins$$anonymous$$d of $$anonymous$$ono. Though, don't take my word on it, it's just something i remember (and human memory is known for it's flaws ^^).
ps: The simple PRNG i posted as answer to the linked question tend to form patterns on a larger scale, though on the other hand it's very simple. It highly depends on the usage. The $$anonymous$$ersenne Twister has a hight quality of "randomness".
I'm happy to be proved wrong but, to my knowledge, the implementation is deter$$anonymous$$istic and empirically I've always found that to be the case. Of course that implementation could change, so perhaps I should have qualified my answer with "given the same seed and version of Unity, Random.value will..." :)
I finally found the old forum post i was searching for ages ^^:
https://forum.unity3d.com/threads/which-random-number-generator-does-unity-use.148601/
Sometimes google isn't really helpful ...
If you really want to ensure to always have the same implementation, just use your own. As Andeeeee said Unity uses (at least back then) Xorshift 128. It's really easy to implement. You could copy&paste 90% of the wikipedia sample code ^^.
edit
I quickly implemented the XorShift64: XorShift64.cs. I've implemented most common methods. If you miss something feel free to add it to your copy ^^. Or write some extension methods.
I tried to auto-initialize the seed in a way it's less "predictable". $$anonymous$$ost directly use DateTime.Ticks or Environment.TickCount. Though both have a bad resolution (that explains the problems most have when they create new Random instances in quick succession). "Stopwatch.GetTimestamp" is said to use a high performance counter if available, otherwise it does the same as DateTime.Ticks. So i just threw that in as well, doesn't hurt.
A bit off-topic, but I've understood that even normal float operations aren't necessarily deter$$anonymous$$istic in C# in general. So if total deter$$anonymous$$ism is really required, some fixed point library is the way to go.
Though I've been wondering how this works in terms of the bigger picture since the actual running code is not C# for example on an iOS device...
Any insight would be appreciated.