GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add extra tests for `random.binomialvariate` by gaogaotiantian · Pull Request #112325 · python/cpython

Conversation

@gaogaotiantian

@gaogaotiantian gaogaotiantian commented Nov 22, 2023 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Currently random.binomialvariate does not check the type of n while the docs clearly states it should be an integer (and mathmatically it should be an integer). This could cause things like random.binomialvariate(3.5, 1) == 3.5. In this PR, a check for the type of n is added.

I also added the check for n == 0. This introduces an extra if check on the common path. n == 0 works without this check, it just requires some extra calculation. I can take it out if we want to make the common path slightly faster.

if not c is changed to if c == 0, which I believe is much better when checking a number against 0.

A couple of test cases are added as well, notably self.assertEqual(B(5, 1e-8), 0), which will trigger the uncovered path in BG method under if c == 0.

  • Issue: Add random.binomialvariate() #81620

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

junbinhuang reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@gaogaotiantian
Add extra checks in random.binomialvariate
c29a796
@gaogaotiantian
gaogaotiantian requested a review from rhettinger as a code owner November 22, 2023 23:07
@bedevere-app bedevere-app Bot added the awaiting review label Nov 22, 2023
@bedevere-app bedevere-app Bot mentioned this pull request Nov 22, 2023
Add random.binomialvariate() #81620
Closed
@blurb-it
📜🤖 Added by blurb_it.
be41745
@rhettinger rhettinger self-assigned this Nov 23, 2023
@rhettinger

rhettinger commented Nov 23, 2023

Copy link
Copy Markdown
Contributor

Is the solving a real problem that you've had or is this just a theoretical issue?

In general, the module shies away from such checks unless we find that they are really necessary. For example, gauss doesn't check to see if the sigma argument is negative. Mostly, these calls need to be short and fast without anything extraneous.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gaogaotiantian

gaogaotiantian commented Nov 23, 2023

Copy link
Copy Markdown
Member Author

I happened to check the coverage of random module today and found some uncovered path which led to this PR.

I agree that the function should be short and fast, but should we keep the same standard? For example, the check for p prevents the function from a infinite loop which is considered necessary, but why do we check for n < 0? That'll just be a case of "invalud input, invalid output". Should we remove that check?

In gammavariate, we check if beta <= 0, that'll be another example of "invalid input, invalid output', as it was just used as a multiplication factor.

However, I understand if you don't want to add the extra 0 and integer check on n. I do think the comparison with 0 makes more sense than not c. Or we can only keep the new test cases.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rhettinger

rhettinger commented Nov 23, 2023

Copy link
Copy Markdown
Contributor

Or we can only keep the new test cases.

I think that would be best.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gaogaotiantian

gaogaotiantian commented Nov 23, 2023

Copy link
Copy Markdown
Member Author

I think that would be best.

Could you share your thoughts about if not c vs if c == 0? That's actually the part I have a rather strong feeling. I think the latter statement is significantly better in this case.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Nov 23, 2023

Copy link
Copy Markdown
Member

#81620 has been closed a long time ago. Could you please open a new issue for discussing the feasibility of such changes?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rhettinger

rhettinger commented Nov 23, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Could you share your thoughts about if not c vs if c == 0?

It's a bit of a personal preference. This module tends to use the former such as the if y in betavariate. The former is also faster than if c == 0 and is consistent with the practice for sequences of writing if not seq instead of if len(seq) == 0. Using the truthy value of an object is common in the Python (and some other languages). It is something you get used to.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rhettinger

rhettinger commented Nov 23, 2023

Copy link
Copy Markdown
Contributor

@serhiy-storchaka I don't think we need to open another issue for a minor code edit. I'll just remove the issue reference from this PR.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rhettinger rhettinger added the skip issue label Nov 23, 2023
@rhettinger rhettinger changed the title gh-81620: Add extra checks in random.binomialvariate Add extra tests for random.binomialvariate Nov 23, 2023
gaogaotiantian added 4 commits November 23, 2023 10:07
@gaogaotiantian
revert changes to binomial variate
00cb3ea
@gaogaotiantian
Remove test for float n
f091c7a
@gaogaotiantian
Update 2023-11-22-23-08-47.gh-issue-81620.mfZ2Wf.rst
b903b89
@gaogaotiantian
Update the test
ce62406
@gaogaotiantian

gaogaotiantian commented Nov 23, 2023

Copy link
Copy Markdown
Member Author

It's a bit of a personal preference. This module tends to use the former such as the if y in betavariate. The former is also faster than if c == 0 and is consistent with the practice for sequences of writing if not seq instead of if len(seq) == 0. Using the truthy value of an object is common in the Python (and some other languages). It is something you get used to.

I'm actually perfectly fine with truthy value check for sequences. I only have hesitations about numerical values. It is faster than c == 0.

Anyway, I removed the code change in random.py and I agree we don't need a separate issue for this minor change.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

rhettinger
rhettinger approved these changes Nov 23, 2023
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Nov 23, 2023
@rhettinger
rhettinger merged commit dc0adb4 into python:main Nov 23, 2023
@bedevere-app bedevere-app Bot removed the awaiting merge label Nov 23, 2023
@rhettinger

rhettinger commented Nov 23, 2023

Copy link
Copy Markdown
Contributor

Thanks for the extra tests and for the polite discussion.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gaogaotiantian
gaogaotiantian deleted the random-binomial branch November 27, 2023 05:54
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
@gaogaotiantian @aisk
Add extra tests for random.binomialvariate (pythongh-112325)
3163db6
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
@gaogaotiantian @Glyphack
Add extra tests for random.binomialvariate (pythongh-112325)
4a49815
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@rhettinger rhettinger rhettinger approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@rhettinger rhettinger

Labels

skip issue

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@gaogaotiantian @rhettinger @serhiy-storchaka
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.