Math

Math object: a built-in js object that manipulates data, that is, an object that does Math operations;

1. random

Syntax: syntax: math.random (); Randomly generate numbers between 0 and 1 that contain 0 but not 1

// Random numbers between 0 and 1 contain 0 but not 1
 var res=Math.random();
 console.log(res);// Randomly generate numbers between 0 and 1
 
// Random number between 1 and 10
var res=parseInt(Math.random()*10);
 console.log(res);
 
// Random number between 10 and 20
/var res = parseInt( Math.random()*10+10);
/console.log(res);

// Random number between 30 and 40
/var res = parseInt( Math.random()*10+30);
/console.log(res);

// Random number between 20 and 40
var res = parseInt( Math.random()*20+20);
console.log(res);

// A random number between 0 and 40
var res = parseInt(Math.random()*40);
console.log(res);

// random number between min-max?? (Before the bag but not after the bag)
var res = parseInt(Math.random()*(max-min)+min);
console.log(res);

// random number between min-max?? (before and after the package) need to expand by one digit
var res = parseInt(Math.random()*(max+1-min)+min);
console.log(res);

// So you can also encapsulate a function that can generate random numbers between min-max including min and Max
function randNum(min,max){
    return parseInt(Math.random()*(max+1-min)+min);
}
randNum();

Copy the code

2. Round the table

var a = 1.5;
var res =  Math.round(a);
console.log(res);
Copy the code

The probability problem of two methods for comparing numbers between random 0 and 10 (two methods before and after wrapping)

// Method 1: use +1 for each number with equal probability
function randNum(min,max){
    return parseInt(Math.random()*(max+1-min)+min);
}
console.log(randNum(0.10));// The probability of each number between 0 and 10 is equal
Copy the code
// If round is used, it is less likely than the middle number
function randNum(min,max){
    return Math.round((Math.random()*(max-min)+min));
}
// 0-0.5 --> 0
// 0.6-1.4999 ---> 1
/ / 1.5-2.4999 - > 2
/ /...
// 9.5-- 9.9999---->10
console.log(randNum(0.10));// The probability of 0 and 10 is low
Copy the code

3. Ceil: Integer up can only process numbers

var a = 1.43434;
var res =  Math.ceil(a);
console.log(res);//2 If 1.1 also takes the value 2
Copy the code

4. Floor: Integer down only deals with mathematical methods that are part of math

var a = 2.9;
var res = Math.floor(a);
console.log(res);
Copy the code

5. Pow: Method of exponentiation

 var res= Math.pow(3.2); 
console.log(res);//9 is execution 3 squared
Copy the code

6. SQRT: Square root: note that this is the root of the second power

var res =  Math.sqrt(36);
console.log(res);/ / 6
Copy the code

7. Max: Take the maximum value of multiple numbers;

Syntax: Max = math.max (value 1, value 2, value 3, value 4);

8. Min: Take the minimum value of multiple numbers

Syntax: Min = math.min (value 1, value 2, value 3);

Abs: Take the absolute value of a number

Syntax: absolute value = math.abs (value);

10. PI PI

console.log( Math.PI); // System predefined

11. ToFixed: Keep decimal places

var a = 3.1415926;
var res = a.toFixed(3);  // Note that toFixed converts numbers to strings
        // Note: toFixed will be rounded
console.log(res);/ / 3.142
Copy the code