arc4random - setting gap between two objects

Hi,


I have two objects that get their Y axis randomised by arc4random_uniform. I want to be able to set a minimum gap of atleast 60px between the two objects where ever they randomise. I could randomise one object and then add 60px to the other objects but I wanted the gap between them to be completely random but no less than 60px.



Below is my code that generates the two objects, but I am in need of a way to set a minimum gap of 60px between them


-(void)GenerateLines {

CGRect BlackTop = Black.superview.bounds;

CGRect BlackBottom = BlackOne.superview.bounds;

CGFloat topMargin = 80;

CGFloat bottomMargin = 140;

CGFloat RandomX, RandomY;

RandomX = arc4random_uniform(500) + topMargin;


CGFloat BlackYMax = BlackTop.size.height-topMargin-bottomMargin-

Black.bounds.size.height/2;

RandomY = arc4random_uniform(BlackYMax) + topMargin;

Black.center = CGPointMake(300, RandomY);

CGFloat TopMargin = 140;

CGFloat BottomMargin = 80;

RandomX = arc4random_uniform(500) + TopMargin;

CGFloat BlackOneYMax = BlackBottom.size.height-TopMargin-BottomMargin-

BlackOne.bounds.size.height/2;

RandomY = arc4random_uniform(BlackOneYMax) + TopMargin;

BlackOne.center = CGPointMake(300, RandomY);

}



Thanks in advance,

Josh

If your available height is 'h', and the objects have heights 'h1' and 'h2', you can set the randomized distance between them as


d = arc4random_uniform (h - h1 - h2 - 60) + 60


Now think of the two objects as locked together, so their combined height is 'd + h1 + h2'. You can compute the position of the combination as:


y = arc4random_uniform (h - d - h1 - h2)


This is how far you'll have to move the combination to randomize its position within the height. That is, you'll position one object's top or bottom at 'y', and the other object 'd' above/below the first. (The exact details depend on whether y is top-down or bottom-up, and which object is closer to y == 0.)

arc4random - setting gap between two objects
 
 
Q