LCOV - code coverage report
Current view: top level - globalscripts - random.js (source / functions) Hit Total Coverage
Test: lcov.info Lines: 9 12 75.0 %
Date: 2023-04-02 12:52:40 Functions: 4 7 57.1 %

          Line data    Source code
       1             : /**
       2             :  * Returns a pair of independent and normally distributed (zero mean, variance 1) random numbers.
       3             :  * Uses the Polar-Rejection method.
       4             :  */
       5             : function randomNormal2D()
       6             : {
       7             :         let s, a, b;
       8           1 :         do
       9             :         {
      10           1 :                 a = 2 * Math.random() - 1;
      11           1 :                 b = 2 * Math.random() - 1;
      12           1 :                 s = a * a + b * b;
      13             :         } while (s>=1 || s==0);
      14           1 :         s = Math.sqrt(-2 * Math.log(s) / s);
      15           1 :         return [a * s, b * s];
      16             : }
      17             : 
      18             : /**
      19             :  * Return a random element of the source array.
      20             :  */
      21             : function pickRandom(source)
      22             : {
      23     1675267 :         return source.length ? source[Math.floor(source.length * Math.random())] : undefined;
      24             : }
      25             : 
      26             : /**
      27             :  * Return a random floating point number in the interval [min, max).
      28             :  */
      29             : function randFloat(min, max)
      30             : {
      31          74 :         return min + Math.random() * (max - min);
      32             : }
      33             : 
      34             : /**
      35             :  * Return a random integer of the interval [floor(min) .. ceil(max)] using Math.random library.
      36             :  *
      37             :  * If an argument is not integer, the uniform distribution is cut off at that endpoint.
      38             :  * For example randIntInclusive(1.5, 2.5) yields 50% chance to get 2 and 25% chance for 1 and 3.
      39             :  */
      40             : function randIntInclusive(min, max)
      41             : {
      42           0 :         return Math.floor(min + Math.random() * (max + 1 - min));
      43             : }
      44             : 
      45             : /**
      46             :  * Return a random integer of the interval [floor(min) .. ceil(max-1)].
      47             :  *
      48             :  * If an argument is not integer, the uniform distribution is cut off at that endpoint.
      49             :  * For example randIntExclusive(1.5, 3.5) yields 50% chance to get 2 and 25% chance for 1 and 3.
      50             :  */
      51             : function randIntExclusive(min, max)
      52             : {
      53           0 :         return Math.floor(min + Math.random() * (max - min));
      54             : }
      55             : 
      56             : /**
      57             :  * Returns a Bernoulli distributed boolean with p chance on true.
      58             :  */
      59             : function randBool(p = 0.5)
      60             : {
      61           5 :         return Math.random() < p;
      62             : }
      63             : 
      64             : /**
      65             :  * Returns a random radians between 0 and 360 degrees.
      66             :  */
      67             : function randomAngle()
      68             : {
      69           0 :         return randFloat(0, 2 * Math.PI);
      70             : }

Generated by: LCOV version 1.14