Random graphs are a vast research area of graph theory, but I just need something
simple for a few visualizations. I want to make graphs that represent a rail network between cities, such that neighbouring cities
are somewhat connected, and there aren't any long edges that jump halfway across the graph.

Points

The cities or nodes of the graph are randomly generated 2D points in [0,1]. But placing the cities purely at random results in
clusters which make it difficult to distinguish individual nodes.

type Point struct {
X, Y float64
}
type Edge struct {
U, V Point
}

I can prevent this by finding the closest existing node before adding another, and rejecting the new point if it would be too close. This is
an O(n²) algorithm which is fine for my small n (but there are interesting possible optimizations).
There's a danger this loop won't terminate, so I added a hard limit.

Edges

The roads or edges of the graph are randomly picked such that shorter edges are preferred, but not exclusively.
For every node, I attempt to add 3 edges to other points. I call this degree in the code, but that's
slightly misleading as we have a chance to add each edge twice. The average
degree of the finished graph ends up
higher than degree.

I sort the points by how far they are from the node in question and give each a 50% chance of being chosen.
Nothing guarantees that the result is a connected graph, but in practice this has not been a problem.