Thanks for the feedback. That's always useful and encouraging to get.

The RandomPezzer type of code is actually very widely used (and generally reliable) for this problem so I guess I was surprised you didn't find something like it online and were maybe looking for something different.

The second last line is redundant, and if for any reason it's used instead of the earlier lr = ... then sometimes can get the wrong result.