I don't know the precise algorithm but it's largely related to your current position and speed, with the time between strikes being based on some sort of counter.
After last strike there will be a number of frames until it's time for a new strike. At that point the game will prepare for the next strike by checking your current position and speed and calculating where you'll be when it's time for the full strike.
I don't know if there is a maximum distance involved, such that the calculated strike can't be further than a certain distance from where you currently are. Sounds like something a speedrunner would know...
A good strategy is to slow down slightly when the calculation takes place and speed up so that you overtake the strike. The lightning will get you if you move at a constant speed, so a variable speed is more effective.
I only know all this from playing the game so much over the years.