node-red-node-random
Advanced tools
| <script type="text/html" data-help-name="random"> | ||
| <p>Generates a random number between a low and high value.</p> | ||
| <p>Generates a random number between a low and high value. Defaults to 1 to 10.</p> | ||
| <p>If left blank <code>from</code> and <code>to</code> can be set dynamically as below.</p> | ||
| <h3>Inputs</h3> | ||
| <dl class="message-properties"> | ||
| <dt>from <span class="property-type">number</span></dt> | ||
| <dd>containing the low value to be used.</dd> | ||
| <dt>to <span class="property-type">number</span></dt> | ||
| <dd>containing the high value to be used.</dd> | ||
| </dl> | ||
| <h3>Details</h3> | ||
| <p>If set to return an integer it can <i>include</i> both the low and high values. | ||
| <code>min <= n <= max</code></p> | ||
| <code>min <= n <= max</code></p> | ||
| <p>If set to return a floating point value it will be from the low value, up to, but | ||
| not including the high value. <code>min <= n < max</code></p> | ||
| not including the high value. <code>min <= n < max</code></p> | ||
| </script> |
| <script type="text/html" data-help-name="random"> | ||
| <p>最小値と最大値との間の乱数を生成します。</p> | ||
| <p>最小値と最大値との間の乱数を生成します。デフォルトは1から10です。</p> | ||
| <p><code>最小</code> や <code>最大</code> を空にした場合は、以下の様に動的に値を設定できます。</p> | ||
| <h3>入力</h3> | ||
| <dl class="message-properties"> | ||
| <dt>from <span class="property-type">数値</span></dt> | ||
| <dd>使用する最小値を含みます。</dd> | ||
| <dt>to <span class="property-type">数値</span></dt> | ||
| <dd>使用する最大値を含みます。</dd> | ||
| </dl> | ||
| <h3>詳細</h3> | ||
| <p>整数値を返すように設定した場合は、乱数には最大値と最小値の両方が<i>含まれます</i>。 | ||
| <code>min <= n <= max</code></p> | ||
| <code>min <= n <= max</code></p> | ||
| <p>浮動小数点値を返すように設定した場合、乱数は最小値から最大値未満の値を含み、最大値は含まれません。 | ||
| <code>min <= n < max</code></p> | ||
| <code>min <= n < max</code></p> | ||
| </script> |
+5
-2
| { | ||
| "name" : "node-red-node-random", | ||
| "version" : "0.2.0", | ||
| "version" : "0.3.0", | ||
| "description" : "A Node-RED node that when triggered generates a random number between two values.", | ||
@@ -22,3 +22,6 @@ "dependencies" : { | ||
| "url": "http://nodered.org" | ||
| } | ||
| }, | ||
| "contributors": [ | ||
| {"name": "@zenofmud"} | ||
| ] | ||
| } |
+2
-2
@@ -34,4 +34,4 @@ <script type="text/html" data-template-name="random"> | ||
| name: {value:""}, | ||
| low: {value:"1"}, | ||
| high: {value:"10"}, | ||
| low: {value: 1,validate:function(v) { return !isNaN(v) || v.length === 0;} }, | ||
| high: {value: 10,validate:function(v) { return !isNaN(v) || v.length === 0;} }, | ||
| inte: {value:"true"}, | ||
@@ -38,0 +38,0 @@ property: {value:"payload",required:true} |
+63
-11
@@ -6,17 +6,69 @@ | ||
| RED.nodes.createNode(this,n); | ||
| this.low = Number(n.low || 1); | ||
| this.high = Number(n.high || 10); | ||
| this.low = n.low | ||
| this.high = n.high | ||
| this.inte = n.inte || false; | ||
| this.property = n.property||"payload"; | ||
| var node = this; | ||
| var tmp = {}; | ||
| this.on("input", function(msg) { | ||
| var value; | ||
| if (node.inte == "true" || node.inte === true) { | ||
| value = Math.round(Math.random() * (node.high - node.low + 1) + node.low - 0.5); | ||
| } | ||
| else { | ||
| value = Math.random() * (node.high - node.low) + node.low; | ||
| } | ||
| RED.util.setMessageProperty(msg,node.property,value); | ||
| node.send(msg); | ||
| tmp.low = 1 // set this as the default low value | ||
| tmp.low_e = "" | ||
| 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.low_e = " From: " + msg.from; // setup to show bad incoming msg.from | ||
| } | ||
| } | ||
| tmp.high = 10 // set this as the default high value | ||
| tmp.high_e = ""; | ||
| 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 | ||
| tmp.high_e = " To: " + msg.to // setup to show bad incoming msg.to | ||
| } | ||
| } | ||
| // if tmp.low or high are not numbers, send an error msg with bad values | ||
| if ( (isNaN(tmp.low)) || (isNaN(tmp.high)) ) { | ||
| this.error("Random: one of the input values is not a number. " + tmp.low_e + tmp.high_e); | ||
| } 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; | ||
| if (tmp.low > tmp.high) { | ||
| value = tmp.low | ||
| tmp.low = tmp.high | ||
| tmp.high = value | ||
| } | ||
| // 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 = 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); | ||
| node.send(msg); | ||
| } | ||
| }); | ||
@@ -23,0 +75,0 @@ } |
+2
-0
@@ -22,2 +22,4 @@ # node-red-node-random | ||
| You can dynamically pass in the 'From' and 'To' values to the node using msg.to and/or msg.from. **NOTE:** hard coded values in the node **always take precedence**. | ||
| **Note:** This returns numbers - objects of type **number**. |
10569
53.53%93
89.8%25
8.7%