# How to randomly connect instances with cylinders using Geometry nodes?

I have this grid of dots I created using Geometry nodes:

(To explain the random positioning, I added a texture of some perlin noise, and then used the displace modifier on the X and Y coordinates).

I would like each sphere to connect to 0-N number of randomly chosen spheres/instances with a line (very thin cylinder where it's possible to set a custom material for it).

With the ability to modify a random seed of what spheres connect to what other spheres so that I can see how different connections look.

Like so:

Preferably with a parameter for the randomly chosen number of connections for each sphere. (So one sphere gets 3 connections to randomly chosen spheres, another gets 8 connections to randomly chosen spheres, and so on), and I can change that so it's a maximum random value of 100 connections and a minimum random value of 30 connections, and so on.

How can I do this with Geometry nodes?

All you have to do is randomly generate integer values between $$0$$ and the number of your points (minus $$1$$), and use the Field at Index node (available since Blender 3.1) to capture the corresponding positions of the points.

By then instantiating lines on those points, and moving the last point of each to your previously captured position, you should essentially get the desired result:

The example here does just that, but both the creation of the points and the connection of the points are done entirely by chance.

To set the number of connections per point also randomly, you can additionally duplicate the created points with a random value:

In this example you can even display the connection as a curve and adjust the curve as you like, if you add this group:

The trick here is that I convert the lines into Bézier curves and influence/move their curve handles with a few parameters.

And here is the entire node group, as well as the blend file:

(Blender 3.6+)

• I can't thank you enough for such a beautiful and comprehensive answer!
– Hmm
Apr 12, 2022 at 4:19