Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by roberto_sc · Aug 12, 2011 at 01:33 PM · mathfixedupdatemathf

Determinism problems: Mathf, Unity.Random VS Math, System.Random

My game must be deterministic, but I'm having problems running my tests in the same machine (!). We are using .NET Math and Random (always same seed, of course), and that can be a reason why I lose determinism in different machines. Is that true? Does Unity assure the same results on different machines if I use Mathf and Unity.Random?

I'm doing everything on FixedUpdate, so any clues of why I'm losing determinism in the same machine would be great.

My checksum simply sums x,y,z of the position of my main GameObjects.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by ScottMcG · Nov 08, 2011 at 10:21 PM

Both System.Math.Random and UnityEngine.Random are platform/machine independent. Assuming you give it the same seed each time, every build of your game on every platform will produce the same random sequence. If you still don't see deterministic results, something is amiss in your code (seeding, race conditions, flow differences, etc). Here are the diffs that I know of... and reasons why I prefer UnityEngine.Random for game code.

  • UnityEngine.Random has static functions / properties. Any time you need a random number, just use UnityEngine.Random.value. System.Math.Random is a regular class that must be instantiated and stored by any classes trying to use it.

  • UnityEngine.Random produces floats, System.Math.Random produces ints or doubles.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Nov 08, 2011 at 10:35 PM 1
Share

For anything that's at all complex, it's better to use System.Random. (Probably that was just a typo, but it's not System.$$anonymous$$ath.Random.) The reason is that UnityEngine.Random is "global"--any call will change the outcome for the next call, so it's not really that hard to screw up your code by adding a function that uses Random, which interferes with the sequence for other functions also using Random. With System.Random, you can have "local" seeds, so to speak, so functions can work independently without having to worry about it. That also makes it easier to mix pseudo-random code with "real" random code.

avatar image roberto_sc · Nov 09, 2011 at 01:39 PM 0
Share

Yes, that's why I ended up creating my own System.Random singleton to use in the whole project without messing with any possible call from outside scripts. Anyways, thanks for the info, Scott$$anonymous$$cG, the indeter$$anonymous$$ism I've been facing were arisen from other reasons, not the Random, like you said.

avatar image tucano · Nov 16, 2011 at 12:44 PM 0
Share

$$anonymous$$ay be is out of topic. But which library use Unity to generate random numbers? Is $$anonymous$$arsene Twister ? Is the local machine random system?

Ciao

avatar image
0

Answer by tucano · Apr 18, 2012 at 09:44 AM

I am working on a library to generate PNRG (pseudo random numbers) with Mersenne Twister: https://github.com/tucano/UnityRandom

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Convert from m/s to m/fixedFrame 1 Answer

Mathf repeat at the end? help! 1 Answer

What is the best way to map the curve of a jump? 1 Answer

Math Parsers for Unity 5.1.4 and .NET 2.0 or Mono for Unity 1 Answer

Limit mouse position so that player launches same distance 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges