samedi 3 octobre 2020

Why Random.Range with preset seed sometimes returns different value?

I have script, that pseudo randomly places buildings on the map, based on coordinates of the city.

        int tryes = 0;
        float rot_jumps = 90;
        
        Random.State init_state = Random.state;
        int x = 0;
        while((ex_small_cnt < ex_small || small_cnt < small || medium_cnt < medium || large_cnt < big || ex_large_cnt < ex_large) && tryes < 3000)
        {
            Random.seed = (int)((x * place.x * place.y) + place.x + place.y + x) + 11;
            if (medium_cnt < medium)
            {
                GameObject house = Instantiate(medium_houses[Random.Range(0, medium_houses.Length)], houses_places[Random.Range(0, houses_places.Count)], Quaternion.Euler(0f, 0f, Random.Range(0, 8) * rot_jumps)) as GameObject;
                house.transform.parent = wholeScene.transform;
                if (house.GetComponent<HouseScript>().Collides()) Destroy(house);
                else { medium_cnt++; buildings.Add(house); }
                x++;
            }
            if(ex_small_cnt < ex_small)
            {
                GameObject house = Instantiate(extra_small_houses[Random.Range(0, extra_small_houses.Length)], houses_places[Random.Range(0, houses_places.Count)], Quaternion.Euler(0f, 0f, Random.Range(0, 8)* rot_jumps)) as GameObject;
                house.transform.parent = wholeScene.transform;
                if(house.GetComponent<HouseScript>().Collides()) Destroy(house);
                else {ex_small_cnt++; buildings.Add(house);}
                x++;
            }
           if (small_cnt < small)
           {
                GameObject house = Instantiate(small_houses[Random.Range(0, small_houses.Length)], houses_places[Random.Range(0, houses_places.Count)], Quaternion.Euler(0f, 0f, Random.Range(0, 8) * rot_jumps)) as GameObject;
                house.transform.parent = wholeScene.transform;
                if (house.GetComponent<HouseScript>().Collides()) Destroy(house);
                else {small_cnt++; buildings.Add(house);}
                x++;
           }
            if (large_cnt < big)
            {
                GameObject house = Instantiate(large_houses[Random.Range(0, large_houses.Length)], houses_places[Random.Range(0, houses_places.Count)], Quaternion.Euler(0f, 0f, Random.Range(0, 8) * rot_jumps)) as GameObject;
                house.transform.parent = wholeScene.transform;
                if (house.GetComponent<HouseScript>().Collides()) Destroy(house);
                else{ large_cnt++; buildings.Add(house); }
                x++;
            }
            if (ex_large_cnt < ex_large)
            {
                GameObject house = Instantiate(extra_large_houses[Random.Range(0, extra_large_houses.Length)], houses_places[Random.Range(0, houses_places.Count)], Quaternion.Euler(0f, 0f, Random.Range(0, 8) * rot_jumps)) as GameObject;
                house.transform.parent = wholeScene.transform;
                if (house.GetComponent<HouseScript>().Collides()) Destroy(house);
                else {ex_large_cnt++; buildings.Add(house); }
                x++;
            }
            tryes++;
        }
        Random.state = init_state;

Everything works perfect, but only for some amount of times I run a game. After(let's say 20) times I play a game, all houses' displacement spontaneously changes and remains same for 20 more plays and than changes once more... I've already checked Random.seed and it remains same. I also know, that Random.Range affects Random.Range, but it's not a problem in my case.




Aucun commentaire:

Enregistrer un commentaire