A Simple Method of Generating Random Numbers Between N and M by JavaScript

  • 2021-07-10 18:50:36
  • OfStack

In this paper, an example is given to describe the simple method of generating random numbers between N and M by JavaScript. Share it for your reference, as follows:

getRandom. js:


/**
 * Created with JetBrains PhpStorm.
 * User: lee
 * To change this template use File | Settings | File Templates.
 */
// Get 1 Random number, in max,min Between 
//max  Need to be greater than  min
// If you enter  10 Is generated  0~9  Number of 
// If you enter  10,4 Is generated  4~9  Number of 
// If you enter  10,40 Is returned  false
function getRandom(max,min){
  var rand=false;
  rand=Math.floor(Math.random()*max);
  if(!(min==undefined) && (max>=min)){
    rand=Math.floor(Math.random()*(max-min)+min);
  }else if(max<min){
    return false;
  }
  return rand;
}

More readers interested in JavaScript can check the topics of this site: "Summary of JavaScript Mathematical Operation Usage", "Summary of JavaScript Data Structure and Algorithm Skills", "Summary of JavaScript Array Operation Skills", "Summary of JavaScript Sorting Algorithm", "Summary of JavaScript Traversal Algorithm and Skills", "Summary of JavaScript Lookup Algorithm Skills" and "Summary of JavaScript Error and Debugging Skills"

I hope this article is helpful to everyone's JavaScript programming.


Related articles: