Underscore: _.random can return double sometimes

Created on 20 Aug 2017  ·  3Comments  ·  Source: jashkenas/underscore

_.random = function(min, max) {
    if (max == null) {
        max = min;
        min = 0;
    }
        // if min is not integer, we can not get integer as return
    return min + Math.floor(Math.random() * (max - min + 1));
}

_.random(5.1, 6) then we will get double not integer return. I think it is a bug because the doc told me I can get a random integer as return

Most helpful comment

I created a pull request for this, see #2696

All 3 comments

min and max should probably be rounded before applying the formula

I created a pull request for this, see #2696

This issue can be closed. See https://github.com/jashkenas/underscore/pull/2696#issuecomment-343276407

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mjeanroy picture mjeanroy  ·  15Comments

wilhen01 picture wilhen01  ·  32Comments

umarfarooq125 picture umarfarooq125  ·  8Comments

wavebeem picture wavebeem  ·  32Comments

grignaak picture grignaak  ·  16Comments