Interesting that so many random color solutions suffer from the same off-by-one error (I left the same comment on the post Our favorite javascript one-liners):
16777215 == 0xffffff, which is the max "value" for a hex color; however, Math.floor(Math.random() * n) gives integers in the range 0..(n - 1), not 0..n. For example, Math.floor(Math.random() * 2) will always give 0 or 1, never 2.
If you want to have the possibility of ever generating pure white (0xffffff), you instead need to use a multiplier of 0xffffff + 1:
Interesting that so many random color solutions suffer from the same off-by-one error (I left the same comment on the post Our favorite javascript one-liners):
16777215 == 0xffffff
, which is the max "value" for a hex color; however,Math.floor(Math.random() * n)
gives integers in the range0..(n - 1)
, not0..n
. For example,Math.floor(Math.random() * 2)
will always give0
or1
, never2
.If you want to have the possibility of ever generating pure white (
0xffffff
), you instead need to use a multiplier of0xffffff + 1
: