Calculate percent at runtime

algorithm, c#, java, language-agnostic, python

Solution

Why not do it randomly. For each transaction, pick a random number between 0 and 100. If that number is less than your "percent", then audit the transaction. If the number is greater than your "percent", then don't. I don't know if this satisfies your requirements, but over an extended period of time, you will have the right percentage audited.

If you need an exact "skip 2, audit one, skip 2 audit one" type of algorithm, you'll likely have luck adapting a line-drawing algorithm.

Problem

I have this problem where I have to "audit" a percent of my transtactions. If percent is 100 I have to audit them all, if is 0 I have to skip them all and if 50% I have to review the half etc. The problem ( or the opportunity ) is that I have to perform the check at runtime. What I tried was: ``` audit = 100/percent ``` So if percent is 50 ``` audit = 100 / 50 ( which is 2 ) ``` So I have to audit 1 and skip 1 audit 1 and skip 1 .. If is 30 audit = 100 / 30 ( 3.3 ) I audit 2 and skip the third. Question I'm having problems with numbers beyond 50% ( like 75% ) because it gives me 1.333, ... When would be the correct algorithm to know how many to audit as they go?... I also have problems with 0 ( due to division by 0 :P ) but I have fixed that already, and with 100 etc. Any suggestion is greatly appreciated.

Original source