Problem of procedural generation using randomness

I have been thinking about this problem for a while but I can’t manage to find where my algorithm is wrong :

I’m working on a procedural generated space game and I want the world to be composed of tiles that would have different functions, the problem is that I want them to be spaced so that the player can go between them, so I made so that they won’t spawn next to each other but when I try to add tiles that are empty, the algorithm is kind acting weird :

Here is my code :
using UnityEngine;
using System.Collections;

public class ChunkManagerS : MonoBehaviour 
{
	public int chunkLength;
	public int spaceRange;
	public int randomness;
	public GameObject [,] primarChunk;
	public GameObject platformPrefab = null;

	void Start ()
	{
		primarChunk = new GameObject[chunkLength,chunkLength];

		for(int x = 0; x < chunkLength; x++)
		{
			for(int z = 0; z < chunkLength; z++)
			{
				bool c = false;

				for(int a = x - spaceRange; a <= x + spaceRange; a++)
				{
					for(int b = z - spaceRange; b <= z + spaceRange; b++)
					{
						if(a >= 0 && b >= 0 && a <= x && b <= z)
						{
							if(primarChunk[a,b] != null)
								c = true;
						}
					}
				}

				if(!c)
				{
					//if(Random.Range(0,100) < randomness)  Randomness system added later
					//{
						primarChunk[x,z] = (GameObject) Instantiate(platformPrefab, new Vector3(x, 0, z), Quaternion.identity);
					//}
				}
			}
		}
	}
}

So everything is working well when I don’t use randomness, for example if I ask for a 100 tiles chunk with a spaceRange of 2 I get this result :

But if I had add the fact that a tile has 50% of chance to be empty, strange stuff happens :

Some tiles’ corners start colliding with other tiles’ corner and it’s not the result that I was looking for so if you have any ideas to resolve this problem I would really appreciate ^^.

Thx in advance !

Your for loops seem to be a complete nonsense, to be honest. You can easily fix them by simply doing the first 2 loops

for(int x = 0; x < chunkLength; x++)
             for(int z = 0; z < chunkLength; z++)

Inside you can simply use this check:

if (primarChunk[a, b] == null && Random.value < randomness)
       primarChunk[x,z] = (GameObject) Instantiate(platformPrefab, new Vector3(x*Random.Range(0,spaceRange+1), 0, z*Random.Range(0, spaceRange+1), Quaternion.identity);

This should check everything and decide if you need to spawn it. No need to put brackets. Also, for this, the randomness definition should look like this:

[Range(0,1)]
public float randomness;

It will have a nice slider.

Finally got a way to go trough this problem and it’s quite simple !

I just took a look at my 2 first for loops and realized that I could not just increment my x and z variables but that I could make them interact with other variables so I did this :

		for(int x = 0; x <= chunkLength; x += spaceRange)
		{
			for(int z = 0; z <= chunkLength; z += spaceRange)
			{
				if(Random.Range(0,100) < randomness)
				{
					primarChunk[x,z] = (GameObject) Instantiate(platformPrefab, new Vector3(x, 0, z), Quaternion.identity);
				}
			}
		}

Now my tiles don’t check anymore if tiles around them are empty or not, they just check if they need to be empty or not using a random number ^^
The 2 for loops handle the format and the position of every tiles now to make it simple.

I still don’t know where my algorithm was wrong but I got another way so I’ll just say that this post is answered.

Thanx for all the answers thought ! ^^