javascript - Math.random and arithmetic shift -
if have following code in javascript:
var index1 = (math.random() * 6) >> 0; var index2 = math.floor(math.random() * 6);
the results index1
or index2
anywhere between 0
, 6
.
i must confused understanding of >>
operator. thought using arithmetic shift results index1
anywhere between 1
, 6
.
i noticing, don't need use math.floor()
or math.round()
index1
if use >>
operator.
i know can achieve adding 1
both indexes, hoping there better way of ensuring results 1
6
instead of adding 1
.
i'm aware bitwise operators treat operands sequence of 32 bits (zeros , ones), rather decimal, hexadecimal, or octal numbers. example, decimal number 9 has binary representation of 1001. bitwise operators perform operations on such binary representations, return standard javascript numerical values.
update:
i saw original usage in caat tutorial on line 26 , wondering whether return random number between 1
, 6
, seems ever return random number between 0
, 6
. never see anim1.png
fish image!
thank in advance enlightenment.
wikipedia says '(arithmetic right shifts negative numbers equivalent division using rounding towards 0 in one's complement representation of signed numbers used historic computers.)'
not answer, idea >> 0 specific , shouldn't used in general getting range between 1 , 6.
most people tell do
math.floor((math.random()*10)+1);
Comments
Post a Comment