Node-RED Random Number Generator
The `node-red-node-random` package provides a Node-RED node that generates random numbers based on configurable minimum and maximum values. It is a fundamental utility node for introducing variability into Node-RED flows, suitable for simulations, testing, or dynamic control systems. The node, currently at version 0.4.1 (though the GitHub repository indicates releases up to 0.8.0), is actively maintained by the Node-RED project. It allows for generating either integers within an inclusive range (`min <= n <= max`) or floating-point numbers within a range that is inclusive of the minimum but exclusive of the maximum (`min <= n < max`). A key differentiator is its ability to accept dynamic 'From' and 'To' values via `msg.from` and `msg.to` properties, although hardcoded node configurations take precedence. It's released as part of the `node-red-nodes` collection, which typically sees updates aligned with Node-RED core releases or as needed for individual nodes.
Common errors
-
Node not found in palette after installation.
cause The Node-RED runtime might not have been restarted, or the node was installed in the wrong directory or failed installation.fixEnsure `npm install node-red-node-random` was run in your Node-RED user directory (typically `~/.node-red`). Restart Node-RED (`node-red-restart` or `pm2 restart node-red` etc.). Check Node-RED logs for installation errors. -
Random number is outside expected range (e.g., maximum value is never reached for floats).
cause Misunderstanding of inclusive vs. exclusive range behavior for integer vs. floating-point number generation.fixRemember that integers include both `min` and `max` (`min <= n <= max`), while floats include `min` but exclude `max` (`min <= n < max`). Adjust your `high` value accordingly. For example, if you want floats up to 10.0 (inclusive), set `high` to 10.000001 (or just above 10). -
msg.from or msg.to values are ignored; the node always uses its configured static range.
cause Hardcoded 'From' and 'To' values in the node's configuration dialog take precedence over incoming `msg.from` and `msg.to` properties.fixTo allow dynamic range setting via `msg.from` and `msg.to`, ensure the 'From' and 'To' fields within the 'Random' node's configuration in the Node-RED editor are left blank.
Warnings
- gotcha When configured for integers, the random range is inclusive of both minimum and maximum values (`min <= n <= max`). For floating-point numbers, the range is inclusive of the minimum but exclusive of the maximum (`min <= n < max`). This difference can lead to off-by-one errors or unexpected results if not accounted for.
- gotcha Values configured directly in the 'Random' node's editor (hardcoded) will always take precedence over `msg.from` or `msg.to` properties passed in the incoming message. This can be misleading if a user expects message properties to dynamically override static settings.
- gotcha Node-RED nodes often update their behavior or dependencies. Because `node-red-node-random` is part of a larger `node-red-nodes` repository, detailed changelogs for individual nodes may not be readily apparent in their npm package, potentially obscuring breaking changes or new features introduced in `Milestone Releases` (e.g., v0.5.0, v0.6.0, v0.7.0, v0.8.0).
Install
-
npm install node-red-node-random -
yarn add node-red-node-random -
pnpm add node-red-node-random
Imports
- Random node (from palette)
import { Random } from 'node-red-node-random'; // Node-RED nodes are not directly imported into user JavaScript files.Install via Node-RED Palette Manager or `npm install node-red-node-random` in your user directory (~/.node-red), then drag the 'Random' node from the palette into a flow.
- Node-RED Flow JSON
const randomNode = new RandomNode(); // Node definitions are internal to Node-RED; users interact with instances in flows.
{ "id": "<node-id>", "type": "random", "name": "My Random Number", "low": "0", "high": "100", "integers": "true" }
Quickstart
[{"id":"d90d8a28.66d5b","type":"inject","z":"a8b2c3d4e5f6g7h8","name":"Trigger Random Number","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":100,"wires":[["e8f9a0b1.c2d3e4f5"]]},{"id":"e8f9a0b1.c2d3e4f5","type":"random","z":"a8b2c3d4e5f6g7h8","name":"Random Integer (1-10)","low":"1","high":"10","integers":"true","property":"payload","x":440,"y":100,"wires":[["f9a0b1c2.d3e4f5a6"]]},{"id":"f9a0b1c2.d3e4f5a6","type":"debug","z":"a8b2c3d4e5f6g7h8","name":"Output Debug","active":true,"toside":"true","console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":700,"y":100,"wires":[]},{"id":"1b2c3d4e.5f6a7b8c","type":"inject","z":"a8b2c3d4e5f6g7h8","name":"Trigger Dynamic Float (0-1)","props":[{"p":"from","v":"0","vt":"num"},{"p":"to","v":"1","vt":"num"},{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":160,"wires":[["2c3d4e5f.6a7b8c9d"]]},{"id":"2c3d4e5f.6a7b8c9d","type":"random","z":"a8b2c3d4e5f6g7h8","name":"Random Float (dynamic)","low":"0","high":"100","integers":"false","property":"payload","x":440,"y":160,"wires":[["3d4e5f6a.7b8c9d0e"]]},{"id":"3d4e5f6a.7b8c9d0e","type":"debug","z":"a8b2c3d4e5f6g7h8","name":"Output Debug Dynamic","active":true,"toside":"true","console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":700,"y":160,"wires":[]}]