Flash tiled map using spacial averaging. (AS2)

I’m trying to make a tiled flash mapping programme using spatial averaging in AS2.

I have a function that fills the screen with random tiles.

I presumed that by running a second check where each tile checks its surround tiles and changes appropiately would work, but it only ever returns 3, 6 or 9 collisions regardless of the actual number.

Any ideas what’s wrong? This is what I tried:

function filterMap()
{
for (x = 0; x < Stage.width; x += scale)
{
for (y = 0; y < Stage.height; y += scale)
{
collisions = 0;
for (j = -scale; j <= scale; j += scale)
{
for (k = -scale; k <= scale; k += scale)
{
if (tile[x + k, y + j] == tile[x, y])
{
collisions++;
}
}
}
if (collisions< 6)
{
changeTile();
}
}
}
}