Permalink
Browse files

Improve random() (#269)

* Improve random() (fixes processing-js/processing-js#244, processing-js/processing-js#256)

* Fix random with 2 args; improve test suite

* Fix test suite as per comments
  • Loading branch information...
1 parent a369605 commit bfcbc2e02026cc2dc81c983310b001ba8871a4bc @CapacitorSet CapacitorSet committed with Pomax Oct 12, 2016
Showing with 13 additions and 7 deletions.
  1. +7 −7 src/P5Functions/Math.js
  2. +6 −0 test/unit/random.pde
@@ -425,14 +425,14 @@ module.exports = function withMath(p, undef) {
* @see randomSeed
* @see noise
*/
- p.random = function() {
- if(arguments.length === 0) {
- return internalRandomGenerator();
+ p.random = function(aMin, aMax) {
+ if (arguments.length === 0) {
+ aMax = 1;
+ aMin = 0;
+ } else if (arguments.length === 1) {
+ aMax = aMin;
+ aMin = 0;
}
- if(arguments.length === 1) {
- return internalRandomGenerator() * arguments[0];
- }
- var aMin = arguments[0], aMax = arguments[1];
if (aMin === aMax) {
return aMin;
}
@@ -49,14 +49,20 @@ var minSample = 30, maxSample = 40;
for(int i=0;i<attempts;++i) {
var sample = r1[i];
if(sample < 0 || sample >= 1) _checkTrue(false); // bad random with no args
+ if(typeof sample !== "number") _checkTrue(false);
+ if(isNaN(sample)) _checkTrue(false);
}
for(int i=0;i<attempts;++i) {
var sample = random(maxSample);
if(sample < 0 || sample >= maxSample) _checkTrue(false); // bad random with one arg
+ if(typeof sample !== "number") _checkTrue(false);
+ if(isNaN(sample)) _checkTrue(false);
}
for(int i=0;i<attempts;++i) {
var sample = random(minSample, maxSample);
if(sample < minSample || sample >= maxSample) _checkTrue(false); // bad random with two arg
+ if(typeof sample !== "number") _checkTrue(false);
+ if(isNaN(sample)) _checkTrue(false);
}
double r,

0 comments on commit bfcbc2e

Please sign in to comment.