Don’t set the random seed; you only set the random seed if you specifically want repeatable numbers (and even then you wouldn’t set it in a loop like that, you’d just set it once). So just get rid of that line entirely.
I’m not even using the values for anything, I’m concatenating them to a string and then printing it right after the loop ends. Same result after the restart.
It’s my own dumb @$$ fault. I can’t believe I did this… but I am using static references, not even thinking about it… I’m modifying 2 static objects via references, so the numbers are only reflecting the last value set to each of the 2 static objects.
Huh, I’m actually seeing the same sort of thing with Random.Next() calls I have in a while loop (it repeats 10 times, currently). I’m using it to randomly pick a prefab to instantiate in my game and it’s picking the same prefab every time.
I also think it’s related to time because if I step through the code it does not repeat values and picks different ones every time, but with no debug and hitting Play on the game, running normal speed, it’s the same value it produces.
I’m not using static references either. I’ve attached the method code in question.
private void SpawnPeople()
{
while (spawnedPeople.Count < 10)
{
System.Random randomPrefabIndex = new System.Random();
System.Random randomXCoordinate = new System.Random();
System.Random randomZCoordinate = new System.Random();
int randomPeoplePrefabIndex = randomPrefabIndex.Next(0, peoplePrefabs.Count - 1);
int randomPeopleXCoordinate = randomXCoordinate.Next(-40, -35);
int randomPeopleZCoordinate = randomZCoordinate.Next(-50, -10);
// get a random person prefab to spawn
GameObject randomPersonPrefab = peoplePrefabs.ElementAt(randomPeoplePrefabIndex);
GameObject personGameObject = Instantiate(randomPersonPrefab, new Vector3((float)randomPeopleXCoordinate, -0.21f, (float)randomPeopleZCoordinate), new Quaternion());
spawnedPeople.Add(personGameObject);
}
}
System.Random gives much different results than UnityEngine.Random.
Doing this nearly always uses the same ‘seed’ in System.Random
while (spawnedPeople.Count < 10)
{
System.Random randomPrefabIndex = new System.Random();
System.Random randomXCoordinate = new System.Random();
System.Random randomZCoordinate = new System.Random();
Instead you probably want this
System.Random randomPrefabIndex = new System.Random();
System.Random randomXCoordinate = new System.Random();
System.Random randomZCoordinate = new System.Random();
while (spawnedPeople.Count < 10)
{