How can I generate a random value but limited between two values such as a value between (25 - 50)? please I want a code similar to this code

msg.payload = {};
msg.payload.DamA = parseInt(Math.random() * 100);
return msg;


msg.payload = {};
msg.payload.DamA = Math.floor(Math.random() * 26) + 25
return msg;

there is a node node-red-node-random that will do all you are asking, I use it and found it works well.


its do not work

it do not work well

What doesn't work about it?

Both the function provided by @E1cid and the node-red-node-random node can do exactly what you have asked for.

If they do not work for you, please explain what is not working so we can understand what is missing.

1 Like
msg.payload = {};
msg.payload.ReservoirCapacity =  parseInt(Math.random() * 25);
msg.payload.DamA   = parseInt(Math.random() * 10);
msg.payload.DamB   = parseInt(Math.random() * 10);
msg.payload.DamC   = parseInt(Math.random() * 10);
msg.payload.AirT   = parseInt(Math.random() * 40);
msg.payload.WaterT = parseInt(Math.random() * 32);
msg.payload.Node01 = parseInt("Normal");
msg.payload.Node02 = parseInt("Normal");
msg.payload.Node03 = parseInt("Normal");
msg.payload.Node04 = parseInt("Normal");
msg.payload.Raspberrypi = parseInt("Normal");
return msg;

when i use "msg.payload.DamA = Math.floor(Math.random() * 26) + 25" all code stop and out nothing

Well i just took the code that E1Cid provided and ran it with an inject node 1/2 dozen times and it worked perfectly.

I would suggest you take the code he provided and run it in a function node and show us the debug output.

You can then take it and put it into your function and isolate where the problem is

I performed my test on NR 1.3.5



Also works fine for me using NR 2.2.2


That will result in NaN (not a number). Suggest to remove it.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.