- Home /
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)];
}
}
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)];
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
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
![](https://koobas.hobune.stream/wayback/20220612075954im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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