Page 78 - JavaScript
P. 78

for(var i = v; i > 0; i --){
                 r += Math.random();
             }
             return r / v;
         }

















        The image shows the distribution of random values for different values of v. The top left is
        standard single Math.random() call the bottom right is Math.random() summed 8 times. This is from
        5,000,000 samples using Chrome


        This method is most efficient at v<5

        Ceiling and Floor


        ceil()


        The ceil() method rounds a number upwards to the nearest integer, and returns the result.

        Syntax:


         Math.ceil(n);


        Example:


         console.log(Math.ceil(0.60)); //  1
         console.log(Math.ceil(0.40)); //  1
         console.log(Math.ceil(5.1));  //  6
         console.log(Math.ceil(-5.1)); // -5
         console.log(Math.ceil(-5.9)); // -5


        floor()

        The floor() method rounds a number downwards to the nearest integer, and returns the result.


        Syntax:


         Math.floor(n);


        Example:


         console.log(Math.ceil(0.60)); //  0
         console.log(Math.ceil(0.40)); //  0
         console.log(Math.ceil(5.1));  //  5



        https://riptutorial.com/                                                                               35
   73   74   75   76   77   78   79   80   81   82   83