Skip to content

std::rand::{gen_int_range,gen_uint_range} #8644

Closed
@sruggier

Description

@sruggier

The gen_uint_range function in std::rand uses the modulo operator to remap generated numbers from [0, uint::max_value] into the range [start, end). If uint::max_value is not a multiple of (end - start), then this technique will result in random numbers of an uneven probability distribution. For small values of (end - start), the error is negligible, but as the size of (end - start) approaches the size of uint::max_value, there starts to be a non-negligible error.

To illustrate the problem, imagine if (start, end) were (0, uint::max_value/3*2). All the random numbers above 2/3 of the max_value would be remapped to the range of [0, uint::max_value/3 ], and it would be twice as probable that the final number is within the first half of the requested range.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions