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 /
  • Help Room /
avatar image
2
Question by Dheeraj20 · Mar 06, 2016 at 01:53 PM · random

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?

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

3 Replies

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

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)

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 psykojello2 · Sep 24, 2017 at 12:20 AM 0
Share

:o

That answer made me rethink all my life choices!

Great example!

avatar image Suduckgames · Nov 22, 2018 at 10:40 AM 0
Share

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?

avatar image roberto_sc · Nov 08, 2019 at 09:21 PM 0
Share

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.

avatar image
2

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/

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
avatar image
1

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.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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