UNPKG

random-generator_node-red-contrib

Version:

A package to meet the randomness generation needs of node-red developers

21 lines (19 loc) 739 B
module.exports = function (RED) { function randomNumberGenerator(config) { RED.nodes.createNode(this, config); var node = this; node.on("input", function (msg) { min = config.minimum == "" ? 0 : parseInt(config.minimum); max = config.maximum == "" ? 1 : parseInt(config.maximum); roundTo = config.roundTo == "" ? 2 : parseInt(config.roundTo); if (min >= max) node.error("minimum value should be less than maximum"); else { msg.payload = config.Floor ? Math.round(Math.random() * max + min) : parseFloat((Math.random() * max + min).toFixed(parseInt(roundTo))); node.send(msg); } }); } RED.nodes.registerType("Number", randomNumberGenerator); };