1

If I have the following code in JavaScript:

var index1 = (Math.random() * 6) >> 0;
var index2 = Math.floor(Math.random() * 6);

The results for index1 or index2 are anywhere between 0 and 6.

I must be confused with my understanding of the >> operator. I thought that by using arithmetic shift that the results for index1 would be anywhere between 1 and 6.

I am noticing, however that I don't need to use Math.floor() or Math.round() for index1 if I use the >> operator.

I know I can achieve this by adding 1 to both indexes, but I was hoping there was a better way of ensuring results are from 1 to 6 instead of adding 1.

I'm aware that bitwise operators treat their operands as a sequence of 32 bits (zeros and ones), rather than as decimal, hexadecimal, or octal numbers. For example, the decimal number nine has a binary representation of 1001. Bitwise operators perform their operations on such binary representations, but they return standard JavaScript numerical values.

UPDATE:

I saw the original usage in this CAAT tutorial on line 26 and was wondering whether that would actually return a random number between 1 and 6 and it seems it would only ever return a random number between 0 and 6. So you would never actually see the anim1.png fish image!

Thank you in advance for any enlightenment.

Charles
  • 50,943
  • 13
  • 104
  • 142
gotnull
  • 26,454
  • 22
  • 137
  • 203
  • 2
    You shouldn't use bitwise operators in javascript. Numbers are implemented as 64 floating points and are internally converted to 32 bit integers for the operation and then back again. http://www.i-programmer.info/programming/javascript/2550-javascript-bit-manipulation.html – mowwwalker Mar 26 '13 at 05:50
  • @Walkerneo thanks for the link. I'll have a good read through. – gotnull Mar 26 '13 at 05:54
  • Note that step 5 of the algorithm for the [The Signed Right Shift Operator](http://www.ecma-international.org/ecma-262/5.1/#sec-11.7.2) converts of the value to an integer using the internal [ToInt32](http://www.ecma-international.org/ecma-262/5.1/#sec-9.5) method. – RobG Mar 26 '13 at 06:17
  • 1
    @Walkerneo—that's a bit of a sweeping statement. Perhaps the OP has not used it wisely in this case, but the bitwise operators have their uses. – RobG Mar 26 '13 at 06:20
  • @RobG, That's true. I meant he shouldn't use them over Math.floor for the sake of efficiency as one might in another language. Out of curiosity, do you have any examples for uses of bitwise operators in javascript? When I learned they were inefficient, I sort of wondered what uses they had in such a high level language. The only thing I can think of is as passing in options to a function, such as doing something like `renderText(LARGE | BOLD | UNDERLINED)` – mowwwalker Mar 26 '13 at 06:28
  • 1
    @RobG, Hmm, from the Skeetman himself: http://stackoverflow.com/a/261073/828584. edit: Still interested in any other uses, if you've come across them. – mowwwalker Mar 26 '13 at 06:31

1 Answers1

1

Wikipedia says '(Arithmetic right shifts for negative numbers would be equivalent to division using rounding towards 0 in one's complement representation of signed numbers as was used by some historic computers.)'

Not exactly an answer, but the idea is that >> 0 is really specific and shouldn't be used in general for getting a range between 1 and 6.

Most people would tell you to do

 Math.floor((Math.random()*10)+1);
  • 1
    For a range of 1 to 6, shouldn't the multiplier be 6? Or use `Math.ceil` and a multiplier of 5. – RobG Mar 26 '13 at 06:15