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
0
Question by Wittz8 · Jan 27, 2020 at 11:15 PM · scripting problemrandom.rangeintstart

Int Value Not Randomizing

I'm trying to randomize and int -- grid.seed. But when I go into play mode, the seed changes, but to the same number depending on what it was originally. For example, if grid.seed = 1, then this code changes it to 4010. If grid.seed = 2, then it changes it to 2621. If grid.seed = 3, then it changes it to 5858. This is really frustrating because I really need this variable to be random, and I don't know why it's reliant at all on what 'grid.seed' is originally. Can anyone help me out and tell me what's going on here?

 namespace Grids2D {
     public class RandomizeSeed : MonoBehaviour
     {
         public Grid2D grid;
         private int seed;
         void Start()
         {
             grid.seed = UnityEngine.Random.Range(1, 10000);
         }
     }
 
 }

Thank you for your time.

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
0

Answer by logicandchaos · Jan 28, 2020 at 12:27 AM

I'm pretty sure this has to do with your seed value. Random number generation only creates psuedu random numbers. A basic way to explain is they made a huge look up table of dice rolls and your seed value is where you start on that table.. So whenever you start with the same seed value you get the same result, a lot of developers will use the system time as the seed value. There is an excellent GDC talk where one of the speakers goes on about a lot of alternatives and how you can use the pseudo random nature of the numbers as a mechanic, like a meta game.

Comment
Add comment · Show 2 · 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 Wittz8 · Jan 28, 2020 at 12:34 AM 0
Share

Thanks a ton for the reply! Do you know of any way to randomize the seed?

avatar image sacredgeometry Wittz8 · Jan 29, 2020 at 12:02 AM 0
Share

Exactly the same way you randomised the number. Stochasticity is normally faked in computing ... to the point at which there are companies which generate higher entropy randomness by fil$$anonymous$$g walls of lavalamps to get over the problem.

avatar image
0

Answer by Bunny83 · Jan 28, 2020 at 12:58 AM

Unity does initialize it's own PRNG with the current system time by default. Keep in mind that Unity only provides a single PRNG. So when you set the seed


So when you use Random.InitState somewhere in your code any Random.Range call after that will follow the same sequence if you provide the same seed each time.


We have no idea what your "Grid2D" class does and what your seed variable in that class actually does. We also don't know about the execution order of your scripts. If for example your Grid2D script also has a Start method which might manipulate Unity random number generator, the result highly depends on the order in which your scripts get executed. By default there is no specific order unless you set the script execution order for certain classes.


In sort: There is not enough information to solve your issue.

Comment
Add comment · Show 1 · 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 Wittz8 · Jan 28, 2020 at 01:16 AM 0
Share

Yeah you're probably right. Grid2D is from the asset store, and I don't fully know how it works. Thanks.

avatar image
0

Answer by Wittz8 · Jan 28, 2020 at 01:19 AM

I figured it out -- not sure why this works. but I added the line Random.InitState(System.DateTime.Now.Millisecond); to the beginning of the void start() function.

 namespace Grids2D {
     public class RandomizeSeed : MonoBehaviour
     {
         public Grid2D grid;
         private int seed;
         void Start()
         {
             Random.InitState(System.DateTime.Now.Millisecond);
             grid.seed = UnityEngine.Random.Range(1, 10000); //randomizes the shape and location of the cells
         }
     }
 
 }

Now it actually randomizes. Thanks for all the input!

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

121 People are following this question.

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

Related Questions

How to store a nullable int into a .json file? 1 Answer

Randomize a spawn position without repition 3 Answers

Random.Range Problems (Errors CS1502 and CS0266) 1 Answer

Pull a random input field from one scene into another 1 Answer

Errors with Random Instantiate Script(Solved) 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