Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
7
Question by alemimi · Dec 25, 2013 at 05:58 PM · randomgeneratingseeda

Generating a good random seed

I made a game where the map is a set of corridors generated randomly using a lot of Random.Ranges, I got no problems until I got to making the multiplayer, I needed a Seed generated by the Host server and then passed to the other player connected (it's a Co-Op game, no more than 2 Players), I succesfully created a Script that resets the seed any time I press the button to reset the Map. However, my map is not random anymore, Unity generates only 3 or 4 random different sequencies and then starts repeating them each time I restart the Map;

Now my question, Can I write a Script which creates a totally random seed that generates always different sequencies of corridors? Or do I need to write my own random seed generator? If I need to, what's the code to make it work?

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
12

Answer by PlasmaByte · Dec 29, 2013 at 12:03 AM

One simple way to solve this is to use the computers time as a seed. That way it's pretty much guaranteed to be different ever time.

Perhaps use:

Random.seed = (int)System.DateTime.Now.Ticks;

Comment
Add comment · Show 8 · 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 ArnoC · Nov 11, 2014 at 11:12 PM 3
Share

Another slightly better option I$$anonymous$$O:

Random.seed = System.Environment.TickCount;

avatar image DiGiaCom-Tech · Jun 16, 2018 at 05:05 PM 1
Share

Both of the above options generate a seed that is sequential (e.g. 1, 2, 3, ...) which may be O$$anonymous$$ (depending on what you are doing) but if you want a seed that is really random then try this ...

 seed = Random.Range(int.$$anonymous$$inValue, int.$$anonymous$$axValue);

... which will give you a random non sequential integer somewhere between -2,147,483,648 and 2,147,483,647.

avatar image Terpal DiGiaCom-Tech · Aug 21, 2018 at 11:07 AM 0
Share

I don't think this will fix OP's issue. Seeding with a random number generated from a pre-seeded Random object will result in even more deter$$anonymous$$istic behavior. The right answer is to use local time, as both @PlasmaByte and @ArnoC suggested.

avatar image Ziplock9000 DiGiaCom-Tech · Oct 17, 2018 at 07:30 PM -1
Share

You're fundamentally not understanding seeding. You're generating a seed from a none seeded random number generator :) All you've done is move the problem one level down.

avatar image Bunny83 Ziplock9000 · Oct 18, 2018 at 12:45 AM 3
Share

Actually Unity's random number generator is already seeded with the current time when you start your game. So if the host want to generate a random seed that should be send to all clients you can simply use Random.Range to generate a seed.

Show more comments
avatar image
1

Answer by ohokke · Aug 23, 2016 at 05:28 AM

The answer by @Aswissrole still gave me non-unique seeds when generating many seeds in a loop. My solution was to use the states of the Unity Random class, to have a single "state" or "generator" for only generating seeds, while not affecting other generator states:

     private static Random.State seedGenerator;
     private static int seedGeneratorSeed = 1337;
     private static bool seedGeneratorInitialized = false;
     public static int GenerateSeed()
     {
         // remember old seed
         var temp = Random.state;
 
         // initialize generator state if needed
         if (!seedGeneratorInitialized)
         {
             Random.InitState(seedGeneratorSeed);
             seedGenerator = Random.state;
             seedGeneratorInitialized = true;
         }
 
         // set our generator state to the seed generator
         Random.state = seedGenerator;
         // generate our new seed
         var generatedSeed = Random.Range(int.MinValue, int.MaxValue);
         // remember the new generator state
         seedGenerator = Random.state;
         // set the original state back so that normal random generation can continue where it left off
         Random.state = temp;
         
         return generatedSeed;
     }

This pretty much guaranteed to generate only unique seeds. Of course, ignoring the infinitesimally small chance a clash is generated. To solve this I'd say keep track of a list and keep generating until a unique number is generated, but in my case that chance is small enough to not need such a list.

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 hukado · Jan 02, 2020 at 06:50 PM 0
Share

This generates the exact same number to me every time

avatar image Bunny83 hukado · Jan 02, 2020 at 07:56 PM 1
Share

That was exactly the point of this answer. He used a fix constant seed "1337" to generate other seeds for different sub systems. They always get the exact same seed every time. That's the point of specifying a seed manually. If you just want a random seed you don't have to do anything since, as we have discussed above, Unity already initializes the PRNG with the current date / time. So every time you start your game you will get a different seed and a different sequence of pseudo random numbers.


There are no good or bad seeds in terms of random numbers. Every seed is just as good as any other seed. The question was about how to initially generate a random seed which should be shared by all players. You could simply use the current time on the host as seed without any issues. Even "restarting" the game shouldn't be an issue.

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

26 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

Related Questions

Vector 3 to float eg V3(1, 2, 3) -> float 123 in JS? 1 Answer

several Random instances 1 Answer

How do re-randomize Random.seed after setting it? 3 Answers

How can I generate random number except one in javascript 2 Answers

How to generate a random seed at a 24 hour interval 1 Answer


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