Skip to content

Fix genDate generator frequency #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2020

Conversation

antoine-fl
Copy link
Contributor

I discovered that the generator was generating random years but the day
was always December 31st, January 31st, December 1st or January 1st (in
frequency order).
This was because of
purescript/purescript-gen#22 which is now
fixed.
However, reading at the current implementation, there is still a slight
skew toward first days of months following a month with only 30 days or
less. This is because genYear <*> genMonth <*> genDay could generate
something like 2020-04-31 which then get converted to 2020-05-01 by
canonicalDate. This means that 2020-05-01 has twice as much chance to
get generated as other dates.

The fix is to pick a day number between 1 and 365 (or 366 for leap
years) instead.

let maxDays = if isLeapYear year then 365 else 364
janFirst = unsafePartial $ fromJust $ exactDate year bottom bottom
days <- Days <<< toNumber <$> chooseInt 0 maxDays
pure $ unsafePartial $ fromJust $ adjust days janFirst
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could consolidate the use of unsafePartial by moving janFirst down:

genDate :: forall m. MonadGen m => m Date
genDate = do
  year <- genYear
  let maxDays = if isLeapYear year then 365 else 364
  days <- Days $ map toNumber $ chooseInt 0 maxDays
  pure $ unsafePartial fromJust do
    janFirst <- exactDate year bottom bottom
    adjust days janFirst

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

I discovered that the generator was generating random years but the day
was always December 31st, January 31st, December 1st or January 1st (in
frequency order).
This was because of
purescript/purescript-gen#22 which is now
fixed.
However, reading at the current implementation, there is still a slight
skew toward first days of months following a month with only 30 days or
less. This is because `genYear <*> genMonth <*> genDay` could generate
something like 2020-04-31 which then get converted to 2020-05-01 by
`canonicalDate`. This means that 2020-05-01 has twice as much chance to
get generated as other dates.

The fix is to pick a day number between 1 and 365 (or 366 for leap
years) instead.
@thomashoneyman thomashoneyman merged commit 4ed0626 into purescript:master Dec 18, 2020
@antoine-fl antoine-fl deleted the gen_date_fix branch December 18, 2020 08:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants