Skip to content

Instantly share code, notes, and snippets.

@niisar
Forked from kerimdzhanov/random.js
Created February 19, 2016 10:18

Revisions

  1. @kerimdzhanov kerimdzhanov revised this gist Feb 5, 2016. No changes.
  2. @kerimdzhanov kerimdzhanov revised this gist Dec 16, 2015. 1 changed file with 15 additions and 5 deletions.
    20 changes: 15 additions & 5 deletions random.js
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,21 @@
    // MIT License

    // @return {float} a random number between min and max
    /**
    * Get a random floating point number between `min` and `max`.
    *
    * @param {number} min - min number
    * @param {number} max - max number
    * @return {float} a random floating point number
    */
    function getRandom(min, max) {
    return Math.random() * (max - min) + min;
    }

    // @return {integer} a random int between min and max
    /**
    * Get a random integer between `min` and `max`.
    *
    * @param {number} min - min number
    * @param {number} max - max number
    * @return {int} a random integer
    */
    function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
    }
    }
  3. @kerimdzhanov kerimdzhanov revised this gist Jun 2, 2015. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions random.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,11 @@
    // @return [float] a random number between min and max
    // MIT License

    // @return {float} a random number between min and max
    function getRandom(min, max) {
    return Math.random() * (max - min) + min;
    }

    // @return [integer] a random int between min and max
    // @return {integer} a random int between min and max
    function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
    }
  4. @kerimdzhanov kerimdzhanov revised this gist Nov 18, 2013. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +0,0 @@
    Note that as numbers in JavaScript are IEEE 754 floating point numbers with round-to-nearest-even behavior,
    these ranges, excluding the one for Math.random() itself, aren't exact, and depending on the bounds it's
    possible in extremely rare cases (on the order of 1 in 262) to calculate the usually-excluded upper bound.
  5. @kerimdzhanov kerimdzhanov created this gist Nov 18, 2013.
    3 changes: 3 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    Note that as numbers in JavaScript are IEEE 754 floating point numbers with round-to-nearest-even behavior,
    these ranges, excluding the one for Math.random() itself, aren't exact, and depending on the bounds it's
    possible in extremely rare cases (on the order of 1 in 262) to calculate the usually-excluded upper bound.
    9 changes: 9 additions & 0 deletions random.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    // @return [float] a random number between min and max
    function getRandom(min, max) {
    return Math.random() * (max - min) + min;
    }

    // @return [integer] a random int between min and max
    function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
    }