An Improved Random Path Length Algorithm for p-i-n and Staircase Avalanche Photodiodes