Skip to content
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

Confusing argument order for random.randint #9573

Open
creativedutchmen opened this issue Aug 16, 2017 · 8 comments · May be fixed by #26106
Open

Confusing argument order for random.randint #9573

creativedutchmen opened this issue Aug 16, 2017 · 8 comments · May be fixed by #26106

Comments

@creativedutchmen
Copy link

Not sure if this is covered anywhere already, feel free to close if this is the case.

Without having the numpy documentation open, and just looking at the argument names for random.randint, the results I got were.. confusing, to say the least.

Without looking at the docs, what would the following code do: random.randint(low=2, size=5)?
My guess would be that it would give back five integers larger than or equal to 2. However, the result was quite different: array([1, 0, 0, 1, 1]).

Wait, so I specified low=2, but got back numbers which are all lower than low? What the...

The docs didn't help much at first, either: "unless high=None, in which case this parameter is one above the highest such integer" - what? Until I stumbled upon this: "If high is None (the default), then results are from [0, low)."

So high and low are switched based on the value of one of the parameters, which makes things very confusing. Much cleaner would be the following function definition:

randint(high, low=0, size=None, dtype='l')

Of course, this would not permit something like randint(1, 2), but randint(low=1, high=2) is arguably more readable anyway. For backwards compatibility, a check could be built in which checks if low is greater than high, in which case the order can be reversed (while raising a warning).

@eric-wieser
Copy link
Member

So using the pep457(?) convention, the function has the signature ([low, ] high), which is perfectly reasonable.

The bug and confusion here is the handling of keyword arguments, not the way in which positional parameters are interpreted (which matches range).

@bashtage
Copy link
Contributor

For backwards compatibility, a check could be built in which checks if low is greater than high, in which case the order can be reversed (while raising a warning).

This isn't fully BC since this currently raises an error, and so an error would change to a warning and a vector output. It might have been clearer if this function was called randrange, although I think it would be less discoverable..

@creativedutchmen
Copy link
Author

Fair points. Would getting rid of the keyword arguments altogether (like range does) be a better solution?

@bashtage
Copy link
Contributor

Definitions should perhaps be (like range)

randint(high, *, size=None, dtype='l')
randint(low, high, size=None, dtype='l')

which aren't quite perfect, but would explain the intent.

@ChaiBapchya
Copy link

ChaiBapchya commented Nov 6, 2018

Instead of re-ordering
it would have been easier if documentation says

high (required)
low (optional, default to 0)

in this way
randint(5) will basically mean randint(high=5) //range is [0,5)

I can create a PR for the same if this makes sense.

@ffranchina
Copy link

ffranchina commented May 26, 2022

Hello, I just came across this issue exactly because I find very confusing the documentation about numpy.random.randint.

The current documentation shows random.randint(low, high=None, size=None, dtype=int) that is quite misleading since one can think that by calling random.randint(a) a will be assigned to low.

I would propose to adopt the same notation used by the official python documentation for the range() function.
The result could be more readable and more clear. In that way it would look like:

random.randint(high, low=0 size=None, dtype=int)
random.randint(low, high, size=None, dtype=int)

Do you think I can directly open a PR for that?

@bashtage
Copy link
Contributor

@ffranchina I think that is a good idea. Open one and let's see how the docs render in the PR CI run.

ffranchina added a commit to ffranchina/numpy that referenced this issue Sep 9, 2022
@limesqueezy
Copy link

Instead of re-ordering it would have been easier if documentation says

high (required)
low (optional, default to 0)

in this way randint(5) will basically mean randint(high=5) //range is [0,5)

I can create a PR for the same if this makes sense.

Still not resolved in version 1.26 docs, i.e. low is required, high is optional which is wrong.

@bashtage bashtage linked a pull request Mar 22, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants