Javascript Course

The Math.random() JavaScript method returns a floating-point random number between 0 (inclusive) and 1 (not included).
This method can be used to generate random integer numbers in JavaScript.
Bellow there are 3 functions with examples to generate random integer numbers with various criterias.


Examples random numbers in JS

1. The randomNr() function from the following example returns a random integer between a minimum and maximum number.
//Returns a random integer between min (inclusive) and max (inclusive)
function randomNr(min, max){
 return Math.floor(Math.random() * (max - min + 1)) + min;
}

//Test
var nr_al = randomNr(1, 9999);
document.write(nr_al); //78 /355 /7098
2. The randNrDigits() function presented in the following example returns a random integer with a specified number of digits.
//Returns a random integer with a specified number of digits (nd)
function randNrDigits(nd){
 var n0 =1;
 for(var i=1; i<nd; i++) n0 +='0';
 return Math.floor((1*n0) + Math.random() * (9*n0));
}

//Test
var nr_al = randNrDigits(4);
document.write(nr_al); //3424
3. The randNrUniqueDigits() function from the following example returns a random integer with a specified number of digits, and the digits do not repeat.
//Returns a random integer with digits that not repeat, with specified number of digits (nd)
function randNrUniqueDigits(nd){
 var ar_nr =[0,1,2,3,4,5,6,7,8,9];
 for(let i = ar_nr.length -1; i >0; i--){
 let j = Math.floor(Math.random() * (i + 1)); 
 [ar_nr[i], ar_nr[j]] = [ar_nr[j], ar_nr[i]];
 }

 return ar_nr.splice(1, nd).join('')*1;
}

//Test
var nr_al = randNrUniqueDigits(4);
document.write(nr_al); //5940

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Generate random numbers in JavaScript

Last accessed pages

  1. Mysql SELECT JOIN tables on two different Databases (4498)
  2. jQuery UI draggable - Drag elements (11448)
  3. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142520)
  4. Using the Bone Tool (4253)
  5. Node.js Move and Copy Directory (20134)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (523)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (62)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)