# Calculate percent at runtime

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.

Asked by:

**Arthur404**| Posted: 27-01-2022

# Answer 1

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.

Answered by:**Kevin134**| Posted: 28-02-2022

# Answer 2

Try this:

1) Keep your audit percentage as a decimal.

2) For every transaction, associate a random number (between 0 and 1) with it

3) If the random number is less than the percentage, audit the transaction.

**Blake653**| Posted: 28-02-2022

# Answer 3

To follow your own algorithm: just keep adding that 1.333333 (or other quotient) to a counter.

Have two counters: an integer one and a real one. If the truncated part of the real counter = the integer counter, the audit is carried out, otherwise it isn't, like this:

```
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
```

Only increment the real counter when its truncated version = the integer counter. Always increment the integer 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;
```

Answered by: **Emma559**| Posted: 28-02-2022

# Answer 4

```
if percent > random.randint(1,100):
print("audit")
else:
print("skip")
```

Answered by: **Aida320**| Posted: 28-02-2022

# Answer 5

If you need to audit these transactions in real time (as they are received) perhaps you could use a random number generator to check if you need to audit the transaction.

So if for example you want to audit 50% of transactions, for every transaction received you would generate a random number between 0 and 1, and if the number was greater than 0.5, audit that transaction.

While for low numbers this would not work, for large numbers of transactions this would give you very close to the required percentage.

This is better than your initial suggestion because if does not allow a method to 'game' the audit process - if you are auditing every second transaction this allows bad transactions to slip through.

Another possibility is to keep a running total of the total transactions and as this changes the total number of transactions that need to be audited (according to your percentage) you can pipe transactions into the auditing process. This however still opens the slight possibility of someone detecting the pattern and circumventing the audit.

Answered by:**Marcus611**| Posted: 28-02-2022

# Answer 6

For a high throughput system the random method is best, but if you don't want randomness, the this algorithm will do the job. Don't forget to test it in a 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
```

Answered by: **Daniel491**| Posted: 28-02-2022

# Answer 7

You can constantly "query" each audit using 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 better resolution.

There is really no need to use random number generator.

Answered by:**Edgar498**| Posted: 28-02-2022

# Answer 8

Not tested, but in the `random`

module there is a function `sample`

. 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 would generate a list `to_be_audited`

which would be a random, non-duplicating sample of the transactions.

**Arthur938**| Posted: 28-02-2022

# Similar questions

python - What exactly does tell() return, and how do I use it to calculate percent of file read?

I am using Python 2.7 on Windows, and I am very new to Python so forgive me if this is an easy one.
Everything that I have read says that tell() returns the "position", which I believe is basically the cursor position that we are currently at in the read. OK, that sounds helpful, but I cannot figure out how to find the total "positions" of the file to calculate a percentage.

python to calculate percent of cars over the speed limit

I'm trying to calculate the percent of cars that go over the speed limit using this code, except there are errors in the second loop and I'm not sure how to use a loop to increment the amount of cars over the speed limit. My end goal is to print out the percent of cars that go above the speed limit. I'm new to programming so any tips or help would be appreciated, thanks :-)
numCars = int(input("Enter the nu...

python - How to calculate percent and add it to the amount

Closed. This question does not meet Stack Overflow guid...

python - Pandas - calculate percent of total given ranges

I'd like get a percentage of the occurrences of speed data falling into a range as a percentage. As an example, 5% of the speed data is between 0 and 5, 10% is between 5 and 10, etc. I'd also like the ability to resample the output into any frequency (entire period, daily, monthly, etc)
I have a DataFrame that looks like this:
df = pd.DataFrame({'id': '1234',
'datetime': pd.date_r...

python - Pandas calculate percent from two data frames

I have two dataframes:
A B C D
X Y 1.0 49.453125
2.0 67.767857
3.0 48.770833
4.0 43.583333
A E F G C H
X Z 1.0 807 1.0 34.375000
2.0 808 1.0 35.375000
1.0 909 2.0 1.750000
2.0 910 2.0 48.750000
Now I would like to calculate the relative percentage of column H in data frame 2 with the corresponding value of...

python - Calculate percent change in a column within a group in Pandas

My dataframe has a Ticker column and a Price column. There are 10 rows per Ticker (with the same Ticker value) but each row has a different Price.
My aim is to calculate the percent change within a Ticker group.
I tried the following but failed:
Test_preds_actual1['Target1_preds_pct_chng'] = 0
Test_preds_actual1.groupby('Ticker')['Target1_preds_pct_chng'] =\
Test_preds_actual1.groupby('Tick...

python - How to calculate the yearly percent return in pandas

So I have been studying the SP500 yearly returns with information downloaded from my quandl subscription. I have used resample() and pct_change() to study the data but my results are not coming as to what is expected for some reason.
sp500_df = quandl.get("MULTPL/SP500_REAL_PRICE_MONTH", authtoken="YOUR OWN AUTH KEY")
sp500_Y_ret_df = sp500_df['Value'].resample('Y').mean().pct_change().dr...

python - Pandas calculate percent growth over rows

I've created the following pandas dataframe and try to calculate the growth in % between the years given in Col2:
Col1
Col2
Jan
Feb
Mrz
Total
A
2019
100
200
300
600
A
2020
200

python - Group by two columns and calculate percent using two different subsets of data in a column

I am looking for a solution but keep getting stuck.
I have a dataframe that contains four columns ID group type value. I would like to group the records by group, value and calculate a percentage based on the values in the type column. Only two values can exist in this column (numer, denom).
My data looks l...

python - how to calculate percent change between columns in pandas?

I am new to Python and pandas. I created a dataframe and want to calculate the percent change between 2 columns. I know of the pct_change function in pandas but that works between rows.
import pandas as pd
import matplotlib.pyplot as plt
UKnewcars = pd.DataFrame({
'Model': ["Diesel", "MHEV Diesel", "Petrol", "MHEV petrol","BEV", "PHEV", "...

python - What's the best way to calculate a 3D (or n-D) centroid?

As part of a project at work I have to calculate the centroid of a set of points in 3D space. Right now I'm doing it in a way that seems simple but naive -- by taking the average of each set of points, as in:
centroid = average(x), average(y), average(z)
where x, y and z are arrays of floating-point numbers. I seem to recall that there is a way to get...

How Python calculate number?

This question already has answers here:

python - Calculate score in a pyramid score system

I am trying to calculate gamescores for a bunch over users and I haven't really got it yet. It is a pyramid game where you can invite people, and the people you invite is placed beneth you in the relations tree.
So if i invite X and X invites Y i get kickback from both of them. Let's say 10%^steps...
So from X i get 10% of his score and 1% from Y, and X get 10% from Y.
So to calculate this i was thi...

How to calculate a mod b in Python?

Is there a modulo function in the Python math library?
Isn't 15 % 4, 3? But 15 mod 4 is 1, right?

To calculate the sum of numbers in a list by Python

My data
466.67
465.56
464.44
463.33
462.22
461.11
460.00
458.89
...
I run in Python
sum(/tmp/1,0)
I get an error.
How can you calculate the sum of the values by Python?

python - How to calculate a date back from another date with a given number of work days

I need to calculate date (year, month, day) which is (for example) 18 working days back from another date. It would be enough to eliminate just weekends.
Example: I've got a date 2009-08-21 and a number of 18 workdays as a parameter, and correct answer should be 2009-07-27.
thanks for any help

python - How to calculate the scrape URL for a torrent

I've read the Bit-torrent specification and done a number of searches, trying to find out how I can get the seeds/peers/downloaded data from a torrent tracker (using Python). I can calculate the info hash from a Torrent no problem, which matches up with the info hash given by various working torrent applications.
However, when I try to get the information from the tracker I either timeout (the tracker is working) o...

datetime - How to use Python to calculate time

I want to write python script that acts as a time calculator.
For example:
Suppose the time is now 13:05:00
I want to add 1 hour, 23 minutes, and 10 seconds to it.
and I want to print the answer out.
How do I do this in Python?
What if date is also involved?

python - Calculate time between time-1 to time-2?

enter time-1 // eg 01:12
enter time-2 // eg 18:59
calculate: time-1 to time-2 / 12
// i.e time between 01:12 to 18:59 divided by 12
How can it be done in Python. I'm a beginner so I really have no clue where to start.
Edited to add: I don't want a timer. Both time-1 and time-2 are entered by the user manually.
Thanks in advance for your help.

python - How to calculate positions of holes in a game board?

I'm making a game with Python->PyGame->Albow and ran into a problem with board generation. However I'll try to explain the problem in a language agnostic way. I believe it's not related to python.
I've split the game board generation into several parts.
Part one generates the board holes.
Holes are contained in a list/array. Each hole object has a mapping of angles relating to other...

# Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python