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
2
Question by saltedsouto · May 02, 2016 at 05:16 AM · c#materialsrandom.range

Random Numbers Always The Same?

Hey guys, I have a set of buildings in my Scene that I want to be a random color. It picks random, but since all Start() calls are at the same time, they all end up the same number. Any ideas? Thanks!

using UnityEngine; using System.Collections;

public class RandomBuildingColor : MonoBehaviour { public Material[] materialList;

 Renderer rend;

 void Start()
 {
     //Picks a random number
     Random.seed = System.DateTime.Now.Millisecond;
     System.Random randomNumber = new System.Random();

     //Uses that random number to change material from materialList
     rend = GetComponent<Renderer>();
     rend.material = materialList[randomNumber.Next(0, materialList.Length)];
 }

}

Comment
Add comment · Show 1
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 chetanmac · May 02, 2016 at 05:22 AM 0
Share

ins$$anonymous$$d of rend.material = materialList[randomNumber.Next(0, materialList.Length)]; use UnityEngine's Random function. It should be like rend.material = materialList[Random.range(0,materialList.length)];

1 Reply

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

Answer by tanoshimi · May 02, 2016 at 06:19 AM

Your code is a bit muddled - you're assigning Random.seed on line 6, (from the UnityEngine namespace, I assume) but then not doing anything with it. Then you're using the System.Random class on line 7, but not setting the seed at all.

If you want a unique generator per object, you can try seeding System.Random using the Object's ID:

 System.Random randomNumber = new System.Random(GetInstanceID());

http://docs.unity3d.com/ScriptReference/Object.GetInstanceID.html

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 Bunny83 · May 02, 2016 at 11:13 AM 0
Share

To get a better seed for each object that isn't bound to the object ID alone, you can simply use some sort of hash function. Just combine the current time with the object ID and create a hash of that ($$anonymous$$D5 for example). The resulting number will be different for every object every time.

However, why not simply using Unity's Random generator and not applying a manual seed. Unity automatiically seeds the generator with the current time, so the resulting random numbers are different each time. Since Unity uses a single Thread for the scripting, all your Start methods are run one after the other and each will get a different number. This of course only works when you don't set the seed in each class. So just remove the assignment of a seed and use Random.Range(0, materialList.Length)

ps: the following line doesn't create a random number, but a random number generator:

 System.Random randomNumber = new System.Random();

so the variable name is misleading. The "Next" method will actually generate the next pseudo-random-number for this generator.

Just to sum up: To get a series of see$$anonymous$$gly random numbers you either have to use:

  • one single PRNG which is seeded only once.

  • multiple PRNGs but ensure that the seed is already a random number.

Since in your original code you create multiple generators and don't provide a seed yourself, it will simply use the current time. If the program runs fast enough some or maybe all generators might be created within the same millisecond and therefore get the same seed.

If you want to seed them manually you shouldn't use milliseconds as they change far too slow. Use the least significant part of the current tickcount. It's pretty impossible for two methods to execute at the same tickcount since the tickcount is incremented each processor tick. I would still recommend, if it's important the values are different, to combine multiple things and create a hash of that data and use the hash as seed.

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Can a mesh vertex have multiple materials? 1 Answer

Is Random.Range() Really Maximally Inclusive? 4 Answers

Image for a quarter of a second 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