Document Reference: TN201707003 - Rev: 4.07 - Last Update: 24-08-2018 15:53 GMT - Downloaded: 07-Jun-2023 08:39 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
Parameter | Type | Description |
---|---|---|
min | Number | The start value of the required range, e.g. the lowest possible value. |
max | Number | The end value of the required range, e.g. the highest possible value. |
Return Value
Type | Description |
---|---|
Number | A random integer value larger or equal to the value of the min parameter and smaller or equal to the value of the max parameter. |
null | The method will return null if:
|
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:
|
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);
}