Hi!
The title says it all.
so can floor(random(5)) == 5
Hi!
The title says it all.
so can floor(random(5)) == 5
Doesnt random pick a value between the limits you provide. If so this line should never return 5 as floor will round everything down to 4. If you use round or ceiling on the other hand that should work.
You should always refer to the doc for questions like this one: random() doc
The doc says:
For example, random(5) returns values between 0 and 5 (starting at zero, and up to, but not including, 5).
but I suppose with round
the probability distribution is not the same when n>1.
e.g., when n = 2,
0.0-0.5
→ 0
(25%)0.5-1.5
→ 1
(50%)1.5-2.0
→ 2
(25%)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.
Why not use java nextInt?
eg
import java.util.Random;
void setup() {
Random rand = new Random();
for (int i = 0; i < 1000; i++) {
println(rand.nextInt(6));
}
}
Yes, @monkstone, that would work.
In your opinion, what would be the relative merits between using Processing functions versus using imported Java functions?
Processing is only really a subset of java, that offers some convenience methods but in may ways it is impure and there’s no need to be constrained by its limitations.
So, given the code from here, these two lines would be equivalent:
println(rand.nextInt(6));
println(floor(random(6));
On another note concerning the use of random numbers, something many of us may have often noticed are statements such as this:
int r = random(255);
int r = random(255);
int r = random(255);
fill(r, g, b);
That is unfortunate, since it misses the opportunity to achieve a full range of colors, leaving out 255
as a possible resulting intensity value. This would be better:
int r = random(256);
int r = random(256);
int r = random(256);
fill(r, g, b);
Alternatively, we could do something like this:
int r = rand.nextInt(256);
int r = rand.nextInt(256);
int r = rand.nextInt(256);
fill(r, g, b);
See the discussion A newbie needs help.
Internally, processing is already using the java.util.random:
Yes but in many ways processing obscures what is going on and leads to muddled thinking. Far better in ruby:-
rand # yields values in range 0..1.0
rand(0..6) # yields ints in inclusive range
rand(0...6) # yields ints in exclusive range
rand(200..255) # yields ints in inclusive range
Concise syntax but there’s more, say you want a random selection from an array in ruby then just sample it
[0, 100, 34, 78].sample
But of course ruby is a bit more crazy/weird because your array can be a bag of all sorts.