Random.Range within a For Loop, disaster.

for (int i = 0; i < Slot.Length; i++)
        {
            Random.seed = i * i;
            Slot[i].Quantity = Random.Range(0,100);
        }

The “random” numbers are: 33, 33, 33, 99, 33, 33, 99, 99, 33.

It’s not a one time thing, each time two numbers are returned. To be fair, those two numbers are returned randomly. :slight_smile:

I have read the docs, Googled the documentation and I don’t think it’s broken… I know I’m missing the concept here.

Nobody? This doesn’t seem like it should be difficult…

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.

–Eric

for (int i = 0; i < Slot.Length; i++)
Slot[i].Quantity = Random.Range(0,100);

Produces the sequence:

36, 23, 36, 23, 36, 23, 23, 36

next run

73, 77, 73, 77, 77, 73, 77, 73, 73

So what am I missing?

I believe it’s related to time, but I’m not sure in what manner.

Get rid of anything anywhere that sets the seed. Maybe restart Unity.

–Eric

I’ll try the restart, since I have no other places where I modify seed. In fact, I didn’t know you could set the seed until a half hour ago.

61, 45, 45, 61, 61, 45, 61, 45, 45
UnityEngine.Debug:Log(Object)
Inventory:Start() (at Assets/Inventory.cs:53)

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.

Is it because random.range uses a time seed?

for (var i = 0; i < 9; i++) {
    Debug.Log (Random.Range(0,100));
}

Produced 60, 16, 79, 47, 23, 73, 41, 92, 10 here, but that’s just random, and it produces different results every time.

–Eric

;_;

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.

1 Like

Well, better that than your computer achieving sentience and deciding to choose specific numbers for itself instead of random ones. :wink:

–Eric

4 Likes

Yeah that was freaking me out…

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)
            {