faker
faker copied to clipboard
feat: add benfordNumber() helper function
This PR adds a benfordNumber()
helper function, which generates a random integer with the given number of digits, following Benford's law. This aims to create more realistic data.
Here's a comparison between datatype.number()
and helpers.benfordNumber()
on the distribution of first digits.
10M Iterations
datatype.number()
:
Digit | Probability | Count |
---|---|---|
1 | 11.1% | 1110411 |
2 | 11.09% | 1108841 |
3 | 11.12% | 1111828 |
4 | 11.11% | 1110808 |
5 | 11.11% | 1111486 |
6 | 11.11% | 1111429 |
7 | 11.11% | 1110752 |
8 | 11.11% | 1111076 |
9 | 11.13% | 1113260 |
helpers.benfordNumber()
:
Digit | Probability | Count |
---|---|---|
1 | 30.99% | 3098875 |
2 | 17% | 1700135 |
3 | 13.01% | 1300570 |
4 | 9.01% | 900650 |
5 | 8% | 799577 |
6 | 7% | 699698 |
7 | 6% | 599856 |
8 | 5% | 499896 |
9 | 4.01% | 400743 |
Usage:
console.log(faker.helpers.benfordNumber(4)) // 7629
Now that the helper function is implemented, what should be done about finance.amount()
?
I'm very new to this, so I'm not sure if this is all that needs to be done. I'm also not sure if this is the best implementation. I was also wondering if the tests are correct, the way it tests to see if it follows Benford's law looks a little hacky.
Closes #1094
Codecov Report
Merging #1099 (2074916) into main (f943b78) will increase coverage by
0.00%
. The diff coverage is100.00%
.
@@ Coverage Diff @@
## main #1099 +/- ##
=======================================
Coverage 99.64% 99.64%
=======================================
Files 2146 2146
Lines 230393 230429 +36
Branches 979 985 +6
=======================================
+ Hits 229582 229619 +37
+ Misses 790 789 -1
Partials 21 21
Impacted Files | Coverage Δ | |
---|---|---|
src/modules/helpers/index.ts | 97.97% <100.00%> (+0.15%) |
:arrow_up: |
src/modules/internet/user-agent.ts | 86.37% <0.00%> (-0.58%) |
:arrow_down: |
src/modules/finance/index.ts | 100.00% <0.00%> (+0.68%) |
:arrow_up: |
We will move this into number module, but this would just come in v8 Because this might not be that far away, we don't want to introduce a planned deprecation now but just wait on #1122
@Shinigami92 Is this still on hold?
@Shinigami92 Is this still on hold?
As #1122 is merged/solved: no, not blocked anymore
@RelatedTitle Could you please update your PR?
Note: There is the currently open PR #1654 that adds weightedArrayElement as a helper method.
I will close this PR as it has become stale. Feel free to reopen if this is a mistake or extract it into a new PR that has a more up-to-date origin/next
ref.