Not a direct answer to your question, but I found this thread quite informative on this subject:
https://www.chiefdelphi.com/forums/s...d.php?t=132758
with what looked like a very clever suggestion from Ether on this.
(But I couldn't track down the thread with the 'homework' Ether assigned in 2014, which I bet would also be interesting).