Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Ensure `_.random` works with large integer values. [closes #921]

  • Loading branch information...
commit ae3cdd3981b39246f1071216aca7438cec5fcac7 1 parent 04754af
@jdalton jdalton authored
Showing with 15 additions and 1 deletion.
  1. +14 −0 test/utility.js
  2. +1 −1  underscore.js
View
14 test/utility.js
@@ -25,6 +25,20 @@ $(document).ready(function() {
equal(_.identity(moe), moe, 'moe is the same as his identity');
});
+ test("random", function() {
+ var array = _.range(1000);
+ var min = Math.pow(2, 31);
+ var max = Math.pow(2, 62);
+
+ ok(_.every(array, function() {
+ return _.random(min, max) >= min;
+ }), "should produce a random number greater than or equal to the minimum number");
+
+ ok(_.some(array, function() {
+ return _.random(Number.MAX_VALUE) > 0;
+ }), "should produce a random number when passed `Number.MAX_VALUE`");
+ });
+
test("uniqueId", function() {
var ids = [], i = 0;
while(i++ < 100) ids.push(_.uniqueId());
View
2  underscore.js
@@ -1019,7 +1019,7 @@
max = min;
min = 0;
}
- return min + (0 | Math.random() * (max - min + 1));
+ return min + Math.floor(Math.random() * (max - min + 1));
};
// List of HTML entities for escaping.
Please sign in to comment.
Something went wrong with that request. Please try again.