var damage = Math.floor(Math.random()*6);
var damage = 1 + Math.floor(Math.random() * 5); // Generate a number `n` such that 0 <= n <= 4 but then add 1 to it
var damage = Math.max(1, Math.floor(Math.random() * 6)); // Pick the larger of 1 and a number `n` (0 <= n <= 5)
var damage = Math.floor(Math.random() * 6) || 1; // 0 is false, so if the random number is 0 that will be `0 || 1` which is 1
In all fairness I'd likely only recommend alpha123's first code block since the second and third weigh it so it's twice as likely to pick 1.
var damage = Math.ceil(Math.random() * 5) || 1;
Code: (javascript) [Select]var damage = 1 + Math.floor(Math.random() * 5); // Generate a number `n` such that 0 <= n <= 4 but then add 1 to it
It's spec'd to return 0 <= n <= 1 that because it's impossible to get a 0 or 1 with a LCRNG, the excersise is left to the reader.
However if that is the case (I don't know much about pseudorandom number generation, so I'll take your word for it) Math.ceil(Math.random() * 5) would work, correct?
var rnd = low + Math.min(Math.floor(Math.random() * (high - low + 1)), high)
However, there's another caveat: Regardless of whether you use floor, ceil or round, due to floating point rounding issues, there's a very small chance (something on the order of 1 in 2^60 IIRC) that you could get a number one higher than your upper bound. For that reason, this is the method I use:Code: (javascript) [Select]var rnd = low + Math.min(Math.floor(Math.random() * (high - low + 1)), high)