Calculate interest at runtime

I have this problem when I have to โ€œcheckโ€ the percentage of my transactions.

If the percentage is 100, I have to check them, if 0, I must skip all of them, and if 50%, I should look at half, etc.

The problem (or possibility) is that I have to run a check at runtime.

What I tried was:

audit = 100/percent 

So, if the percentage is 50

 audit = 100 / 50 ( which is 2 ) 

So, I need to audit 1 and skip 1 audit 1 and skip 1 ..

If 30

audit = 100/30 (3.3)

I check 2 and skip the third.

Question

I have problems with numbers above 50% (e.g. 75%) because it gives me 1.333, ...

When is the correct algorithm to find out how many of them need to be checked when they go? ... I also have problems with 0 (due to dividing by 0: P), but I fixed it already with 100, etc. d.

Any suggestion is much appreciated.

+6
java python language-agnostic c # algorithm
source share
8 answers

Why not do it randomly. For each transaction, select a random number from 0 to 100. If this number is less than your "percentage", then audit the transaction. If the number is greater than your "percent", then no. I do not know if this suits your requirements, but for a long period of time you will have the correct percentage checked.

If you need the exact algorithm "skip 2, audit one, skip 2 audit one", you will most likely be able to adapt the drawing algorithm .

+17
source share

Try the following:

1) Keep your verification percentage as decimal. 2) For each transaction, match a random number (between 0 and 1) with it
3) If the random number is less than a percent, check the transaction.

+3
source share
  if percent > random.randint(1,100): print("audit") else: print("skip") 
+2
source share

To follow your own algorithm: just keep adding 1.333333 counter (or other factor).

There are two counters: integer and real. If the truncated part of the real counter = integer counter, an audit is performed, otherwise it is not:

 Integer counter Real counter 1 1.333333: audit transaction 2 2.666666: audit transaction 3 3.999999: audit transaction 4 truncated(5.333333) = 5 > 4 => do NOT audit transaction 5 5.333333: audit transaction 

Increase the real counter only when its truncated version = integer counter. Always increase the numerical counter.

In code:

 var p, pc: double; c: integer; begin p := 100 / Percentage; pc := p; for c := 1 to NrOfTransactions do begin if trunc(pc) = c then begin pc := pc + p; Do audit on transaction c end end; end; 
+2
source share

If you need to audit these transactions in real time (as you receive them), you can probably use a random number generator to check if you need to check the transaction.

So, if you want, for example, to check 50% of transactions, for each received transaction you will create a random number from 0 to 1, and if the number is more than 0.5, audit this transaction.

Although this will not work for low numbers, for a large number of transactions it will give you very close to the required percentage.

This is better than your initial proposal, because if the โ€œgameโ€ method does not allow during the audit process - if you check every second transaction, it allows you to skip bad transactions.

Another possibility is to keep the total number of transactions as a whole and, since this changes the total number of transactions that need to be checked (according to your percentage), you can translate the transactions into the audit process. However, this still opens up a small possibility that someone will discover the pattern and bypass the audit.

+1
source share

For a high-bandwidth system, the random method is best, but if you don't want randomness, this algorithm will do the job. Remember to check it on unit test!

 // setup int transactionCount = 0; int auditCount = 0; double targetAuditRatio = auditPercent/100.0; // start of processing transactionCount++; double actualAuditRatio = auditCount/transactionCount; if (actualAuditRatio < targetAuditRatio) { auditCount++; // do audit } // do processing 
+1
source share

You can constantly โ€œqueryโ€ each audience using a counter. for example

 ctr = 0; percent = 50 while(1) { ctr += percent; if (ctr >= 100) { audit; ctr = ctr - 100; } else skip } 

You can use floats (however this will bring some unpredictability) or multiply 100 percent by sth to get a better resolution.

There is no need to use a random number generator.

0
source share

Not tested, but there is a sample function in the random module. If transactions was a list of transactions, you would do something like:

 import random to_be_audited = random.sample(transactions,len(transactions*100/percentage)) 

This will create a to_be_audited list, which will be a random, non-duplicate transaction pattern.

See documentation for random

0
source share

All Articles