Question about node-red-node-random

#1

According to the README for node-red-node-random, in floating-point mode this node gives a random output, x, that is strictly between the given maximum and minimum values, that is, min < x < max. However, everything I've read about the Math.random() method (for instance, https://www.w3schools.com/jsref/jsref_random.asp) implies that the lower limit is included, that is min ≤ x < max. The difference might seem trivial, but it does matter for a flow I'm developing. Is the README wrong, or have I mis-read something in the code?

0 Likes

#2

Even more interesting is the help screen

It behaves differently based on what you are asking the output to be formatted as !!

image

Craig

0 Likes

#3

Here is the actual JS could explain some of the reasons

module.exports = function(RED) {
    "use strict";
    function RandomNode(n) {
        RED.nodes.createNode(this,n);
        this.low = Number(n.low || 1);
        this.high = Number(n.high || 10);
        this.inte = n.inte || false;
        this.property = n.property||"payload";
        var node = this;
        this.on("input", function(msg) {
            var value;
            if (node.inte == "true" || node.inte === true) {
                value = Math.round(Number(Math.random()) * (node.high - node.low + 1) + node.low - 0.5);
            }
            else {
                value = Number(Math.random()) * (node.high - node.low) + node.low;
            }
            RED.util.setMessageProperty(msg,node.property,value);
            node.send(msg);
        });
    }
    RED.nodes.registerType("random",RandomNode);
}

Craig

0 Likes

#4

Yes it behaves differently on purpose.
And yes in float mode it does include the minimum.

The thinking being that for non computer folk, if I want say a dice to give me a number from 1 to 6 I would ask for that not 1 to 7.

1 Like

#5

Thanks, Dave. Would you mind correcting the documentation?

I also wonder about using the Number() method in the code. Is that just to protect against some sort of wonkiness in the random number generator?

0 Likes

#6

done. pushed as v0.1.1

1 Like

#7

Thanks, but (oops!) not fixed in the README...

0 Likes

#8

do people read them ? :wink: v0.1.2

0 Likes

#9

We can always hope :grimacing:

0 Likes