Page 74 - JavaScript
P. 74

var power = Math.pow(10, places);
              return Math.floor(value * power) / power;
          }


        Random Integers and Floats


         var a = Math.random();


        Sample value of a: 0.21322848065742162


        Math.random() returns a random number between 0 (inclusive) and 1 (exclusive)


         function getRandom() {
             return Math.random();
         }


        To use Math.random() to get a number from an arbitrary range (not [0,1)) use this function to get a
        random number between min (inclusive) and max (exclusive): interval of [min, max)


         function getRandomArbitrary(min, max) {
           return Math.random() * (max - min) + min;
         }


        To use Math.random() to get an integer from an arbitrary range (not [0,1)) use this function to get a
        random number between min (inclusive) and max (exclusive): interval of [min, max)


         function getRandomInt(min, max) {
           return Math.floor(Math.random() * (max - min)) + min;
         }


        To use Math.random() to get an integer from an arbitrary range (not [0,1)) use this function to get a
        random number between min (inclusive) and max (inclusive): interval of [min, max]


         function getRandomIntInclusive(min, max) {
           return Math.floor(Math.random() * (max - min + 1)) + min;
         }


        Functions taken from https://developer.mozilla.org/en-
        US/docs/Web/JavaScript/Reference/Global_Objects/Math/random


        Bitwise operators


        Note that all bitwise operations operate on 32-bit integers by passing any operands to the internal
        function ToInt32.


        Bitwise or


         var a;
         a = 0b0011 | 0b1010; // a === 0b1011


        https://riptutorial.com/                                                                               31
   69   70   71   72   73   74   75   76   77   78   79