This program is basically a single line minesweeper game. All I have here is the process of selecting where to put mines and displaying them.... but the subroutine to do that is messed up somewhere....can anyone tell me where?

It's meant to place:
MMM, MM, MM, M, M, M at random in the array "grid"... there must be at least one space between them though
all others are marked as E..... so I set them all to E and just changed certain ones to M.

What happens is that they sometimes appear next to each other...even though they shouldnt... and I only get "MMM", "MM", and "M"..... but am missing two single Ms and one MM.

Code follows:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void placemines (char *grid, int number, int reps) {

	int i, check = 0, minepos = 0, count, flag, endcheck;

	for (count = 1; count <= reps; count++) {

		int maxpos = 26 - number; // Sets maximum starting position

		flag = number + 2;
		endcheck = number + 1;

		while (flag >= 1) {

			srand((unsigned)time(NULL));
			minepos = rand() % maxpos + 1; // Picks random start position

			for (i = -1; i <= endcheck; i++) {

				check = minepos + i;
				if (grid[check] == 'E') flag--;
				else if (grid[check] == 'M') flag++;

			}

		}

		for (i = 0; i < number; i++) {

			grid[minepos + i] = 'M'; // Set mines

		}

	}

}

int main () {

	char grid[29], display[27]; // Set necessary arrays..
	grid[28]='\0';
	char play = 'y'; // Toggle for another game, yes by default 
	int i, letter; // Set's necessary int variables

	while (play == 'y') { // Enters playing loop.

	    /* Initialisation Process */

		for (i = 0; i <= 27; i++) {

			grid[i]='E'; // Set's grid[0] to grid[27] to 'e' for 'empty'.

		}

		for (i = 1, letter = 'a'; i <= 26; i++, letter++) {

			display[i] = letter; // Set up a-z display array.

		}

		placemines(grid,3,1); // Initialise mines, it's not pretty but it works
		placemines(grid,2,2);
		placemines(grid,1,3);


		/* End Initialisation Process */

		printf("Welcome To Entirely $$$$ 1D Minesweeper\n");

		for (i = 1; i <= 26; i++) {

			printf("  %c", display[i]); // Show letters

		}

		printf("\n");



		for (i = 1; i <= 26; i++) {

			printf("  %c", grid[i]); // Show grid

		}

		play = 'n'; // To stop it looping until I put in the exit option

	}

	return 0;

}