When using random()
with floor()
, round()
or ceil()
, we have to consider carefully what we would like the likelihoods of the numbers within the designated range to be.
As noted above, the upper bound is exclusive. So random(5)
, picks numbers between 0.0
and 5.0
, with 5.0
never occurring.
With random(5)
, we could get exactly 0.0
on extremely rare occasions. So it is important to be aware that with ceil(random(5))
, we could get either 0
, 1
, 2
, 3
, 4
, or 5
, but 0
would only occur extremely rarely. Each of the other numbers, including 5
, would have about a 20% chance of occurring.
With round(random(5))
, we could get either 0
, 1
, 2
, 3
, 4
, or 5
. However, 1
, 2
, 3
, and 4
would each have a 20% chance of occurring, while 0
and 5
would each have about a 10%
probability of occurring.
Therefore, first we need to decide what outcomes we want to be possible, including the desired probabilities of each number within the targeted range. Then we need to decide carefully what range to pass to random()
and how to use floor()
, round()
, or ceil()
to achieve the desired probability distribution.
EDIT (April 9, 2021):
If we want an integer result, with each number equally likely, we can achieve it, but it needs to be done carefully. floor()
can be useful for this.