1.) Randomizing the amount of items.
static const int MinNumBlocks = 4;
static const int MaxNumBlocks = 6; // must be greater than MinNumBlocks
sf::Sprite leftBlock[MaxNumBlocks];
int numBlocks = MinNumBlocks + rand() % ( MaxNumBlocks - MinNumBlocks );
for (int idxBlock = 0; idxBlock < numBlocks; idxBlock++) {
...
}
2.) Ensuring a minimal distance between items by relocating if the minimum distance to any already existing item is fallen below a threshold.
static const float SquaredMinDistance = 20.0f;
static const int MaxNumTrials = 10;
for (int idxBlock = 0; idxBlock < numBlocks; idxBlock++) {
for (int trial = 0; trial < MaxNumTrials; trial++) {
x = rand() % 400 + 60;
y = rand() % 400 + 60;
bool okay = true;
for (int idxCheck = 0; idxCheck < idxBlock; idxCheck++) {
float xDist = x - leftBlock[ idxCheck ].getPosition().x;
float yDist = y - leftBlock[ idxCheck ].getPosition().y;
okay = okay && (( xDist * xDist + yDist * yDist ) >= SquaredMinDistance );
}
if( okay ) {
break;
}
}
leftBlock[ idxBlock ].setTexture( BLOCK );
leftBlock[ idxBlock ].setPosition( x, y );
}
(Its all untested code, but it should show the idea.)