Member Login:


Generates a random integer number value

Document Reference: TN201707003 - Rev: 4.07 - Last Update: 24-08-2018 15:53 GMT - Downloaded: 19-Mar-2024 10:47 GMT

The mcrSf.numRandom() method will take two values, a minimum and a maximum value, and generate an integer value between these two values.

The random() method from the Math object returns a random floating-point number in the range 0 to 0.9 (0.99999...). With a little more coding, this method is generally enough to generate any required random number. The mcrSf.numRandom() method provides this little bit of extra coding.

Parameters

ParameterTypeDescription
minNumberThe start value of the required range, e.g. the lowest possible value.
maxNumberThe end value of the required range, e.g. the highest possible value.

Return Value

TypeDescription
NumberA random integer value larger or equal to the value of the min parameter and smaller or equal to the value of the max parameter.
nullThe method will return null if:
  • min parameter value is larger than max parameter value
  • min parameter value is not a number
  • max parameter value is not a number
  • min parameter value is an empty string
  • max parameter value is an empty string
  • min parameter value is null
  • max parameter value is null

The Stand-Alone Function

Below code is ready for copy/paste and will work just fine on it's own, without linking the mcrSf library file.

 function numRandom(min, max) {
    if (min > max || isNaN(min) || isNaN(max) || min === "" || max === "" ||
        min === null || max === null) {return null;}
    return Math.floor(Math.random() * (max - min + 1) + min);
}

The mcrSf Library Method

 /**
* generates a random number between two values (min and max)
* @version 0.3
* @param {Number} min - start value of specified range (lowest possible value)
* @param {Number} max - end value of specified range (highest possible value)
* @return {Number || null} - random value between min and max (inclusive), or
*     null if not successful
*
* mcrSf.numRandom(0, 10)
* returns >= 0 <= 10 //e.g. any one integer number between 0 and 10 inclusive
* mcrSf.numRandom(6, 10)
* returns >= 6 <= 10 //e.g. any one integer number between 6 and 10 inclusive
* mcrSf.numRandom(0, 0)
* returns 0
* mcrSf.numRandom(10, 10)
* returns 10
* mcrSf.numRandom(-5, 5)
* returns >= -5 <= 5 //e.g. any one integer number between -5 and 5 inclusive
* mcrSf.numRandom(10, 0)
* returns null
* mcrSf.numRandom('ABC', 10)
* returns null
* mcrSf.numRandom(0, 'ABC')
* returns null
* mcrSf.numRandom('', 10)
* returns null
* mcrSf.numRandom(0, '')
* returns null
* mcrSf.numRandom(null, 10)
* returns null
* mcrSf.numRandom(0, null)
* returns null
*
* STRATEGY: Avail of Math.floor(Math.random() * x + y) formulae
*/
numRandom: function(min, max) {
    if (min > max || isNaN(min) || isNaN(max) || min === "" || max === "" ||
        min === null || max === null) {return null;}
    return Math.floor(Math.random() * (max - min + 1) + min);
}

Version History

Revision
Date
Details
0.3
18-Aug-18
Minor release. Calling mcrSf.numRandom(0, 10) returned null.
Updated numRandom() method:
  • Replaced comparison operators in line 37 and 38 from == (equal) to === (strict equal).
0.2
05-Aug-17
Minor release. Renamed randomNum() to numRandom().
0.1
30-Jul-17
First release, randomNum().

Script Archive

Rev 0.2, 05-Aug-17

 /**
* generates a random number between two values (min and max)
* @version 0.2
* @param {Number} min - start value of specified range (lowest possible value)
* @param {Number} max - end value of specified range (highest possible value)
* @return {Number || null} - random value between min and max (inclusive), or
*     null if not successful
*
* mcrSf.numRandom(0, 10)
* returns >= 0 <= 10 //e.g. any one integer number between 0 and 10 inclusive
* mcrSf.numRandom(6, 10)
* returns >= 6 <= 10 //e.g. any one integer number between 6 and 10 inclusive
* mcrSf.numRandom(0, 0)
* returns 0
* mcrSf.numRandom(10, 10)
* returns 10
* mcrSf.numRandom(-5, 5)
* returns >= -5 <= 5 //e.g. any one integer number between -5 and 5 inclusive
* mcrSf.numRandom(10, 0)
* returns null
* mcrSf.numRandom('ABC', 10)
* returns null
* mcrSf.numRandom(0, 'ABC')
* returns null
* mcrSf.numRandom('', 10)
* returns null
* mcrSf.numRandom(0, '')
* returns null
* mcrSf.numRandom(null, 10)
* returns null
* mcrSf.numRandom(0, null)
* returns null
*
* STRATEGY: Avail of Math.floor(Math.random() * x + y) formulae
*/
numRandom: function(min, max) {
    if (min > max || isNaN(min) || isNaN(max) || min == "" || max == "" ||
        min == null || max == null) {return null;}
    return Math.floor(Math.random() * (max - min + 1) + min);
}

Rev 0.1, 30-Jul-17

 /**
* generates a random number between two values (min and max)
* @version 0.1
* @param {Number} min - start value of specified range (lowest possible value)
* @param {Number} max - end value of specified range (highest possible value)
* @return {Number} - random value between min and max (inclusive)
*
* mcrSf.randomNum(0, 10)
* returns >= 0 <= 10 //e.g. any one integer number between 0 and 10 inclusive
* mcrSf.randomNum(6, 10)
* returns >= 6 <= 10 //e.g. any one integer number between 6 and 10 inclusive
* mcrSf.randomNum(0, 0)
* returns 0
* mcrSf.randomNum(10, 10)
* returns 10
* mcrSf.randomNum(-5, 5)
* returns >= -5 <= 5 //e.g. any one integer number between -5 and 5 inclusive
* mcrSf.randomNum(10, 0)
* returns null
* mcrSf.randomNum('ABC', 10)
* returns null
* mcrSf.randomNum(0, 'ABC')
* returns null
* mcrSf.randomNum('', 10)
* returns null
* mcrSf.randomNum(0, '')
* returns null
* mcrSf.randomNum(null, 10)
* returns null
* mcrSf.randomNum(0, null)
* returns null
*
* STRATEGY: Avail of Math.floor(Math.random() * x + y) formulae
*/
randomNum: function(min, max) {
    if (min > max || isNaN(min) || isNaN(max) || min == "" || max == "" ||
        min == null || max == null) {return null;}
    return Math.floor(Math.random() * (max - min + 1) + min);
}

This website uses cookies to give you the best experience on our website, to personalise content and to analyse our website traffic. Some cookies may have been set already. To find out more about our use of cookies you can visit our Privacy Statement. By browsing this website, you agree to our use of cookies.

Hide this message