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

Improve Random{NumberGenerator}.GetItems/String for non-power of 2 choices #107988

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Commits on Sep 19, 2024

  1. Improve Random{NumberGenerator}.GetItems/String for non-power of 2 ch…

    …oices
    
    In .NET 9, we added an optimization to Random.GetItems and RandomNumberGenerator.GetItems/GetString that special-cases a power-of-2 number of choices that's <= 256. In such a case, we can avoid many trips to the RNG by requesting bytes in bulk, rather than requesting an Int32 per element. Each byte is masked to produce the index into the choices.
    
    This PR extends that optimization to also cover non-power-of-2 choices. It can't just mask off the bits as in the power-of-2 case, but it can mask off some bits and then do rejection sampling, which on average still yields big wins.
    stephentoub committed Sep 19, 2024
    Configuration menu
    Copy the full SHA
    c193357 View commit details
    Browse the repository at this point in the history