From 48db5bb829e4b3e4632f0d64a5908746583f4f82 Mon Sep 17 00:00:00 2001 From: juggledad Date: Wed, 2 Dec 2020 06:30:41 -0500 Subject: [PATCH] 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 --- function/random/random.js | 60 +++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/function/random/random.js b/function/random/random.js index aaa73aa6..0348f85a 100644 --- a/function/random/random.js +++ b/function/random/random.js @@ -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); } });