This is a typical geometric probability problem. This type of problems are discussed in the book Counting (Combinatorics).
(1249) Let $S$ be a square of side length $1$. Two points are chosen independently at random on the sides of $S$. Find the probability that the straight-line distance between the points is at least $\dfrac{1}{2}$.