Randomized tile map(not randomizing)

I am attempting to set up the world map in my game with each tile being one of 5 different types. The problem is that when I run the program all of the tiles are exactly the same.

Here is my code

[SerializeField]
    GameObject[] tiles=new GameObject[5];

	void Start () {
        GenerateMap();
	}

    void GenerateMap()
    {
        for(int x = -10; x < 10; x++)
        {
            for(int y=-10; y<10; y++)
            {
                if(!(x<3 && x>-3 && y>-3 && y <3))
                Instantiate(tiles[TileSelect()], new Vector3(x, y), new Quaternion());
            }
        }
    }

    int TileSelect()
    {
        System.Random r = new System.Random();
        int selection = r.Next(5);
        return selection;
    }

The problem might come from creating a new system.random every time you need a random number. I had an issue like this a long time ago in a non-unity program and the solution was to use one instance of System.Random() instead of creating a new one every time you want another number.

Apparently, the random number generator uses the system clock to supply seed values to instances of the Random class. If the loop in your code executes fast enough, each Random instance will get the same seed value and you will wind up getting the exact same “random” number out of each instance. This could explain why your map is always made of the same tile.

Try moving the random declaration out of the TileSelect method and into the class body and see if that fixes it.

You could also use the Unity random number generator instead. You would just replace:

Instantiate(tiles[TileSelect()], new Vector3(x, y), new Quaternion());

with

Instantiate(tiles[Random.Range(0,5)], new Vector3(x, y), new Quaternion());