Good programmers web front-end tutorial Share JavaScript Math (arithmetic) objects, here small share will look dry day for everyone. Well, today is a good programmer, said web front-end training courses chapters.
JavaScript Math (arithmetic) objects
Role Math (arithmetic) objects are: to perform common arithmetic tasks.
Online examples
round()
How to use the round ().
random()
How to return a random number between 0 and 1 using random ().
max()
How to use a larger count max () to return the two given. (Prior ECMASCript v3, only two parameters of the method.)
(I)
How to use the min () returns the number two smaller given number of. (Prior ECMASCript v3, only two parameters of the method.)
Math object
Role Math (arithmetic) objects are: to perform ordinary arithmetic task.
Math object types and a variety of numerical calculation functions. No need to define it before using this object.
Use Math attribute / method syntax:
var x = Math.PI;
var y = Math.sqrt (16);
Note: Math object does not have to define it before using this object.
Count value
JavaScript provide 8 can be accessed Math object count values:
You can refer to the following Javascript constant use:
Math.E
Math.PI
Math.SQRT2
Math.SQRT1_2
Math.LN2
Math.LN10
Math.LOG2E
Math.LOG10E
Arithmetic method
In addition to the object can be accessed Math count value, there are several functions (methods) may be used.
The following example uses the method of the Math object to round a rounded number.
document.write(Math.round(4.7));
The above code is output:
5
The following example uses the Math object random () method to return a random number between 0 and 1:
document.write(Math.random());
The above code is output:
0.4321440459646637
The following example uses the object Math floor () method and random () returns a random number between 0 and 11:
document.write(Math.floor(Math.random()*11));
The above code is output:
5
JavaScript Boolean (Boolean) objects
JavaScript RegExp Object
1 notes
whi***[email protected]
For the pseudo-random number, JS there are many games are played generates a pseudo-random number we need.
The upper and lower limits generates a random number:
var rand = (min,max) => Math.round(Math.random()*(max-min))+min;
// Max is a maximum value, Min is the minimum value
Bool value randomly generated based on probability:
function randBool(percent=0.5){
// percent probability of default 0.5 (50%).
if (Math.random () <percent) // if the random number is less than the probability value, returns true, otherwise returns false.
return true;
else
return false;
}
Designated randomly generated characters:
function randChar(length,characters="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"){
// length to the desired length, characters for all characters included in the default letters + numbers.
characters = characters.split ( ""); // split character.
result = ""; // return result.
while(result.length<length) result+=characters[Math.round(Math.random()*characters.length)];
return result;
}
Randomly generated characters in another play:
function randCharAnother(length,rangeMin=0x80,rangeMax=0x7FF){
// length length, rangeMin minimum Unicode code, rangeMax maximum of Unicode.
result="";
while(result.length<length) result+=String.fromCharCode(Math.round(Math.random()*(rangeMax-rangeMin))-rangeMin);
return result;
}
A stuff taken randomly from the array:
Array.prototype.pick = function(){
// can not () => {/ * function * /}, otherwise this will point Window.
return this.length Math.round (Math.random () * (this.length-1)): undefined; // If the length is 0, return undefined?.
}