faker icon indicating copy to clipboard operation
faker copied to clipboard

feat: add benfordNumber() helper function

Open RelatedTitle opened this issue 2 years ago • 2 comments

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

RelatedTitle avatar Jun 21 '22 09:06 RelatedTitle

Codecov Report

Merging #1099 (2074916) into main (f943b78) will increase coverage by 0.00%. The diff coverage is 100.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:

codecov[bot] avatar Jun 21 '22 17:06 codecov[bot]

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 avatar Aug 25 '22 18:08 Shinigami92

@Shinigami92 Is this still on hold?

ST-DDT avatar Dec 11 '22 01:12 ST-DDT

@Shinigami92 Is this still on hold?

As #1122 is merged/solved: no, not blocked anymore

Shinigami92 avatar Dec 11 '22 10:12 Shinigami92

@RelatedTitle Could you please update your PR?

ST-DDT avatar Dec 11 '22 11:12 ST-DDT

Note: There is the currently open PR #1654 that adds weightedArrayElement as a helper method.

ST-DDT avatar Dec 11 '22 15:12 ST-DDT

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.

xDivisionByZerox avatar May 14 '23 10:05 xDivisionByZerox