How should I connect two LEGO cylinders with pistons to measure the movement caused by sea waves? As part of a school project I need to make a model of a snake electricity generator.
I would like to build this with LEGO elements, and for that, I need to measure the movements of the cylinders relative to each other by mean of the piston connecting them.

I have an extensive LEGO collection, but I have difficulties imagining the best configuration so that the piston has the maximum moving range.
In addition, the system needs to be floating, as the movement of the cylinders is caused by waves at the surface of the water.
It's difficult to suggest parts without knowing what sets are in your collection.  Here are some thoughts to help with assembling this project.


Notice that the snake consists of the following pattern:
float tube -&gt; pump section -&gt; float tube
The float tubes are the elements that keep the heavier pump sections afloat.  You'll need something similar to recreate this with Legos.  You could put balloons inside of the float sections instead of making the Lego portion watertight.  
The float tube / pump section pattern is connected at right angles for each subsequent section.  This is the key in allowing the snake to flex in three dimensions (X, Y, Z).  There are a number of parts that you could use attached to the outside of cylinders to create this connection.
The arms driving the pistons are encased in a flex conduit that is larger than the arm itself.  It's hard to see in that diagram, but the arm driving the pump never bends.  Said another way, the arm only moves in one dimension - back and forth.  I don't know of any Lego parts that would allow for this configuration.  A technics rod plus a flex conduit would look pretty close to what you need.  But the Lego pieces with holes are generally sized to allow the rod with a pretty close fit.  And you need something with a sloppy fit in this case.
Measuring the motion is going to be challenging.  My first attempt would be to put a spring loaded slider against the piston along with a ruler next to the slider.  You could then measure the motion assuming you find a solution to the fit issue from #3 above.  If you wanted to convert that motion to an electrical measurement, you'll need some sort of transducer.  A mindstorms motor might do the job, but I have no idea about how to program and monitor for that type of a thing.
Finally, decide upon what aspect you want to measure to indicate the motion.  The edges of the tubes are actually moving in two planes of motion.  As the edge moves away from the piston, it rises up a little bit to accommodate the pivot point.