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

random has bug #921

Closed
faller opened this issue Dec 26, 2012 · 2 comments
Closed

random has bug #921

faller opened this issue Dec 26, 2012 · 2 comments
Labels

Comments

@faller
Copy link

faller commented Dec 26, 2012

in underscore
_.random = function(min, max) {
if (max == null) {
max = min;
min = 0;
}
return min + (0 | Math.random() * (max - min + 1));
};

return value may lte min, I found this by testing new Date().getTime() as args

the code (0 | Math.random() * (max - min + 1)) with
( 0 | 11531563792 ) returns -1353338096

@jvfl
Copy link

jvfl commented Dec 26, 2012

Well, It's because of the bitwise operator (it only supports 32-bit integers). Don't know if it's a design decision in favor of speed( since that bitwise op is a bit faster than Math.floor), but using Math.floor instead of the bitwise op inside the function should do the job for you.

@faller
Copy link
Author

faller commented Dec 27, 2012

Klimetiel , you got the answer.

Number(11531563792).toString(2).length returns 34

lodash.js use Math.floor instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants