Skip to content

Uniform<f32/f64> should honor high/low limits #476

Closed

Description

@sicking

We should require that all Uniform distributions stick to the high/low limits, even in the face of rounding issues. Users are likely to see unexpected and hard-to-reproduce bugs if the returned value on rare occasions will be outside of the specified limits.

One example that I ran into was while implementing choose_weighted for dhardy#82. If the generated number can be slightly higher than high, that means that we risk iterating through the whole item-set without selecting any item to return.

The simplest approach is to simply check the generated value against high/low and loop if it doesn't fit withing the desired boundaries. However that runs into problem when scale rounds to infinity, since then we'll just loop forever.

Looking into some alternative approaches instead.

Activity

  1. added a commit that references this issue on May 25, 2018
    839ebdb
  2. dhardy commented on May 25, 2018

    @dhardy
    Member

    Probably either rejection sampling or clamping will be needed...

    I'd rather see a complete solution for one particular problem (e.g. choose_weighted), and then discuss whether to adjust Uniform sampling. Use guard functions for the sampling if necessary.

  3. pitdicker commented on May 25, 2018

    @pitdicker
    Contributor

    I'd rather see a complete solution for one particular problem (e.g. choose_weighted), and then discuss whether to adjust Uniform sampling.

    Just wanted to write the same. Good that you thought about this corner case! But I can see other users complaining about the performance drop in gen_range, for a reason they don't cure much about. Maybe it is better to do the rejection sampling outside of gen_range?

    @dhardy It can be a simple function, but what do you think of some method to make rejection sampling extra simple (i.e. that allows users to not write the loop)? Not thought it through though...

  4. dhardy commented on May 25, 2018

    @dhardy
    Member

    So something like Uniform::new(low, high).sample_strict(&mut rng)? I suppose that could work.

  5. pitdicker commented on May 25, 2018

    @pitdicker
    Contributor

    I was thinking more in the direction of:

    pub trait Distribution<T> {
        /* ... */
    
        fn rejection_sample<R: Rng + ?Sized>(&self, rng: &mut R, min: T, max: T)
            -> T where T: PartialOrd<T>;
    }
    
    let val = Uniform::new(low, high).rejection_sample(&mut rng, low, high);

    Hmmm, doesn't look great. Maybe some iterator adapter:

    let val = Uniform::new(low, high).sample_iter(&mut rng).accept_if(|x| x >= low && x < high);

    Also ugly. And does the same as:

    let val = Uniform::new(low, high).sample_iter(&mut rng).filter(|x| x >= low && x < high).next();
  6. dhardy commented on May 25, 2018

    @dhardy
    Member

    Yes, a new method on Uniform was the only way I could think of not having to specify the limits twice.

    Not sure if it's worth it, but it could be an unobtrusive layer on top of the current sample at least.

  7. added 5 commits that reference this issue on Jun 8, 2018
    a4c6b65
    8828cad
    3b1ab6e
    c1cbf32
    d2d4fcd
  8. added 5 commits that reference this issue on Jul 2, 2018
    28b3fbf
    42f79af
    47f2998
    e487112
    9527481
  9. added 4 commits that reference this issue on Jul 12, 2018
    be10dba
    4f8601b
    ccb3c3f
    6d2b7e5
  10. added a commit that references this issue on Jul 12, 2018
    515cf18
  11. added a commit that references this issue on Jul 20, 2018
    1d8cfd7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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