Skip to content

BUG: fix Generator.choice ignoring the shuffle arg#31215

Open
Anarion-zuo wants to merge 2 commits intonumpy:mainfrom
Anarion-zuo:aaron/fix_31210
Open

BUG: fix Generator.choice ignoring the shuffle arg#31215
Anarion-zuo wants to merge 2 commits intonumpy:mainfrom
Anarion-zuo:aaron/fix_31210

Conversation

@Anarion-zuo
Copy link
Copy Markdown
Contributor

PR summary

Closes #31210.

This PR fixes Generator.choice(..., replace=False, p=..., shuffle=...) so that shuffle is respected in the weighted sampling path.

Right now, when replace=False and p is provided, shuffle=True and shuffle=False produce the same ordering. That is different from the unweighted path, where shuffle does affect the output order. In practice, this means the draw order can reflect the internal weighted sampling order instead of being randomized, which is exactly the behavior reported in the issue.

The code change is small: after the weighted no-replacement sampler finishes building the selected indices, it now applies the same final shuffle step that is already used in the unweighted no-replacement path when shuffle=True.

I also added a regression test to check that:

  • weighted sampling without replacement now changes order when shuffle=True
  • the selected elements are still the same set
  • the existing deterministic test for the non-shuffled weighted path continues to work by making it explicitly use shuffle=False

Tests run in Docker:

  • spin test numpy/random/tests/test_generator_mt19937.py -- -q -k "test_choice_nonuniform_noreplace or test_choice_nonuniform_noreplace_shuffle or test_choice_uniform_noreplace"
  • full Docker test run with:
    • spin build --clean -- -Dallow-noblas=true -Dcpu-baseline=none -Dcpu-dispatch=none
    • spin test -- --durations=10 --timeout=600

The focused random tests passed. The full test run completed except for the known environment-specific failure in numpy/_core/tests/test_cpu_features.py::Test_X86_Features::test_features.

I also reproduced the issue visually using the same kind of setup described in the issue report and confirmed that the fixed shuffle=True behavior removes the order correlation, while preserving the sampled set.

AI Disclosure

I used ChatGPT in preparing this pull request.

AI was used for grammar and wording fixes in PR text, and earlier I did not fully disclose that use. I’m sorry about that. It was not intentional.

For completeness: AI was also used to help inspect the code path and help draft text while I was working on the issue. The actual code changes, testing, and submission are being handled by me.

Signed-off-by: aaronzuo <anarionzuo@outlook.com>
@jorenham jorenham closed this Apr 11, 2026
@jorenham jorenham reopened this Apr 11, 2026
@jorenham
Copy link
Copy Markdown
Member

Oh I missed the AI disclosure since it looked identical. The PR description still doesn't look human-written though...

@Anarion-zuo
Copy link
Copy Markdown
Contributor Author

Anarion-zuo commented Apr 11, 2026

Oh I missed the AI disclosure since it looked identical. The PR description still doesn't look human-written though...

The written text can be reused. No need to rewrite things again. AI is great for us not fluent in Eng...

@jorenham
Copy link
Copy Markdown
Member

We prefer to talk to humans, and don't like wasting our time reading very wordy descriptions. I'm sure you understand.

Signed-off-by: aaronzuo <anarionzuo@outlook.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Generator.choice(replace=False, p=...) silently ignores shuffle parameter

3 participants