Skip to content

uniform_int_distribution misbehaves if the range exceeds the range of the underlying rng_engine #1

@athas

Description

@athas

For example, this program will not terminate:

import "lib/github.com/diku-dk/cpprandom/random"

module rng = minstd_rand
module dist = uniform_int_distribution u32 rng

let main (seed: i32) =
  let r = rng.rng_from_seed [seed]
  in dist.rand (rng.min,rng.max+1) r

The problem is this loop where it tries to ensure that the distribution is not unbalanced. If the range is too great, then secure_max becomes 0, and the loop becomes infinite.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions