Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Dec 5, 2018. It is now read-only.

Update Math.js to fix Issue 256 #257

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions src/P5Functions/Math.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,40 +411,39 @@ module.exports = function withMath(p, undef) {

/**
* Generates random numbers. Each time the random() function is called, it returns an unexpected value within
* the specified range. If one parameter is passed to the function it will return a float between zero and the
* value of the high parameter. The function call random(5) returns values between 0 and 5 (starting at zero,
* the specified range. If one parameter is passed to the function it will return a float between zero and
* that value. The function call random(5) returns values between 0 and 5 (starting at zero,
* up to but not including 5). If two parameters are passed, it will return a float with a value between the
* parameters. The function call random(-5, 10.2) returns values starting at -5 up to (but not including) 10.2.
* To convert a floating-point random number to an integer, use the int() function.
*
* @param {int|float} value1 if one parameter is used, the top end to random from, if two params the low end
* @param {int|float} value2 the top end of the random range
* @param {int|float} value1 if one parameter is used, the end of the range, if two params the beginning
* @param {int|float} value2 the end of the random range
*
* @returns {float}
*
* @see randomSeed
* @see noise
*/
p.random = function() {
if(arguments.length === 0) {
return internalRandomGenerator();
p.random = function(a, b) {
a = a || 0;
if (arguments.length === 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with named arguments, we can simplify a little more:

if (arguments.length === 1) {
  b = a;
  a = 0;
} else {
  a = a || 0;
  b = b || 1;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You cannot do b = b || 1; ! That well mess up invocations like random(-5, 0);

Copy link
Member

@Pomax Pomax Aug 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is a good point. But we can do b = (typeof b !== 'undefined') ? b : 1 (really wish there was a pragma that made truthiness easier in JS in that respect...)

Copy link
Contributor

@GoToLoop GoToLoop Aug 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to rely on operator typeof for parameter variables. We can test for 'em directly.
Also, it's much safer to catch null as well by using != instead of !==:
b = b != null? b : 1 or b = b != undefined? b : 1 or b = b != void 0? b : 1

Other alternatives:
b = b == null && 1 || b or b = b == undefined && 1 || b or b = b == void 0 && 1 || b

Or even the good old if ():
if (b == null) b = 1 or if (b == undefined) b = 1 or if (b == void 0) b = 1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, the different ways to it correctly is only slightly less than all the ways to do it incorrectly. So I'm leaving my second iteration as is.

b = 1;
} else if (arguments.length === 1) {
b = a;
a = 0;
}
if(arguments.length === 1) {
return internalRandomGenerator() * arguments[0];
}
var aMin = arguments[0], aMax = arguments[1];
if (aMin === aMax) {
return aMin;
}
for (var i = 0; i < 100; i++) {
var ir = internalRandomGenerator();
var result = ir * (aMax - aMin) + aMin;
if (result !== aMax) {
return result;
if (a !== b) {
for (var i = 0; i < 100; i++) {
var ir = internalRandomGenerator();
var results = ir * (b - a) + a;
if (results !== b) {
return results;
}
// assertion: ir is never less than 0.5
}
// assertion: ir is never less than 0.5
}
return aMin;
return a;
};

// Pseudo-random generator
Expand Down