Above you see my UIView
which is a rectangle with a black border. In the middle is the center (red dot) and furthermore, 2 circles.
My goal is to generate a random X and Y point with a minimum and maximum offset based on the center of the UIView
. The green circle gives away the minimum X and Y positions, while the red outer circle indicates the maximum X and Y positions. My goal is to generate an X and Y value between those two circles.
Lets say I have a UIView
which size is 100,100 and I want to get a random X and Y value with a minimum offset which is 80% of the view (green circle) and a maximum offset of 120% (red circle).
My bad attempt to generate a random X value:
var finalX = CGFloat.random(lower: heightUIView * 0.8, heightUIView * 1.2)
let toDown = Int.random(lower: 0, 1) == 0 ? true : false
if !toDown {
finalX = -finalX
}
This would never generate a good X value since these values can not be 0 (while that could be an option, looking at the picture)