fixed code setting defaults, added floor() and ceil() if result to be integer

I also added a status showing the from and to. This will help people see what is actually being used. i.e you add a random node then attach an inject to send msg.from as 4, you would think the result would be between 4 and 10 but since you did not clear out the 'From' value in the node, the result is from 1 to 10
This commit is contained in:
juggledad 2020-12-02 06:30:41 -05:00 committed by GitHub
parent 78bb05cc04
commit 48db5bb829
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,27 +12,36 @@ module.exports = function(RED) {
var tmp = {};
this.on("input", function(msg) {
this.status({}); // blank out the status on a deploy
if (node.low) { // if the the node has a value use it
tmp.low = node.low
} else { // if 'from' in the msg, use it or default to 1
tmp.low = ('from' in msg) ? msg.from : 1;
}
if (node.high) { // if the the node has a value use it
tmp.high = node.high
} else { // if 'to' in the msg, use it or default to 1
tmp.high = ('to' in msg) ? msg.to : 10;
}
tmp.low = 1 // set this as the default low value
if (node.low) { // if the the node has a value use it
tmp.low = node.low
} else if ('from' in msg) { // else see if a 'from' is in the msg
if (Number(msg.from)) { // if it is, and is a number, use it
tmp.low = Number(msg.from);
} else { // otherwise setup NaN error
tmp.low = NaN
}
}
tmp.high = 10 // set this as the default high value
if (node.high) { // if the the node has a value use it
tmp.high = node.high
} else if ('to' in msg) { // else see if a 'to' is in the msg
if (Number(msg.to)) { // if it is, and is a number, use it
tmp.high = Number(msg.to);
} else { // otherwise setup NaN error
tmp.high = NaN
}
}
// if tmp.low or high are not numbers, send an error msg
if ( (isNaN(tmp.low)) || (isNaN(tmp.high)) ) {
this.error({node:"random",text:"one of the input values is not a number"})
}
else {
// force tmp.high/low to Numbers since they may be a string
tmp.low = Number(tmp.low)
tmp.high = Number(tmp.high)
this.status({fill:"red",shape:"dot",text:"random: from " + Number(tmp.low) + " to " + Number(tmp.high)})
this.error("Random: one of the input values is not a number")
} else {
// at this point we have valid values so now to generate the random number!
// flip the values if low > high so random will work
var value = 0;
@ -42,19 +51,22 @@ module.exports = function(RED) {
tmp.high = value
}
// if returning an integer, do a parseInt() on the low and high values
// before generate the random number. This must be done to insure the rounding
// doesn't go up if using something like 4.7 or you can end up with 5
// if returning an integer, do a math.ceil() on the low value and a
// Math.floor()high value before generate the random number. This must be
// done to insure the rounding doesn't round up if using something like 4.7
// which would end up with 5
if ( (node.inte == "true") || (node.inte === true) ) {
tmp.low = parseInt(tmp.low);
tmp.high = parseInt(tmp.high);
tmp.low = Math.ceil(tmp.low);
tmp.high = Math.floor(tmp.high);
// use this to round integers
value = Math.round(Math.random() * (tmp.high - tmp.low + 1) + tmp.low - 0.5);
} else {
// use this to round floats
value = (Math.random() * (tmp.high - tmp.low)) + tmp.low;
}
RED.util.setMessageProperty(msg,node.property,value);
this.status({fill:"grey",shape:"dot",text:"random: from " + tmp.low + " to " + tmp.high});
node.send(msg);
}
});