Javascript functions math random

Math

The Math namespace object contains static properties and methods for mathematical constants and functions.

Math works with the Number type. It doesn’t work with BigInt .

Description

Unlike most global objects, Math is not a constructor. You cannot use it with the new operator or invoke the Atomics object as a function. All properties and methods of Math are static.

Note: Many Math functions have a precision that’s implementation-dependent.

This means that different browsers can give a different result. Even the same JavaScript engine on a different OS or architecture can give different results!

Static properties

Euler’s number and the base of natural logarithms; approximately 2.718 .

Natural logarithm of 10 ; approximately 2.303 .

Natural logarithm of 2 ; approximately 0.693 .

Base-10 logarithm of E ; approximately 0.434 .

Base-2 logarithm of E ; approximately 1.443 .

Ratio of a circle’s circumference to its diameter; approximately 3.14159 .

Square root of ½; approximately 0.707 .

Square root of 2 ; approximately 1.414 .

The initial value of the @@toStringTag property is the string «Math» . This property is used in Object.prototype.toString() .

Static methods

Returns the absolute value of x .

Returns the arccosine of x .

Returns the hyperbolic arccosine of x .

Returns the hyperbolic arcsine of a number.

Returns the arctangent of x .

Returns the arctangent of the quotient of its arguments.

Returns the hyperbolic arctangent of x .

Returns the cube root of x .

Returns the smallest integer greater than or equal to x .

Returns the number of leading zero bits of the 32-bit integer x .

Returns the hyperbolic cosine of x .

Returns e x , where x is the argument, and e is Euler’s number ( 2.718 …, the base of the natural logarithm).

Returns subtracting 1 from exp(x) .

Returns the largest integer less than or equal to x .

Returns the nearest single precision float representation of x .

Returns the square root of the sum of squares of its arguments.

Returns the result of the 32-bit integer multiplication of x and y .

Returns the natural logarithm (㏒e; also, ㏑) of x .

Returns the base-10 logarithm of x .

Returns the natural logarithm (㏒e; also ㏑) of 1 + x for the number x .

Returns the base-2 logarithm of x .

Returns the largest of zero or more numbers.

Returns the smallest of zero or more numbers.

Returns base x to the exponent power y (that is, x y ).

Returns a pseudo-random number between 0 and 1 .

Returns the value of the number x rounded to the nearest integer.

Returns the sign of the x , indicating whether x is positive, negative, or zero.

Returns the hyperbolic sine of x .

Returns the positive square root of x .

Returns the hyperbolic tangent of x .

Returns the integer portion of x , removing any fractional digits.

Examples

Converting between degrees and radians

The trigonometric functions sin() , cos() , tan() , asin() , acos() , atan() , and atan2() expect (and return) angles in radians.

Since humans tend to think in degrees, and some functions (such as CSS transforms) can accept degrees, it is a good idea to keep functions handy that convert between the two:

function degToRad(degrees)  return degrees * (Math.PI / 180); > function radToDeg(rad)  return rad / (Math.PI / 180); > 

Calculating the height of an equilateral triangle

If we want to calculate the height of an equilateral triangle, and we know its side length is 100, we can use the formulae length of the adjacent multiplied by the tangent of the angle is equal to the opposite.

An equilateral triangle where a perpendicular of one edge is drawn from the opposite vertex, forming a right triangle with three sides marked as

In JavaScript, we can do this with the following:

We use our degToRad() function to convert 60 degrees to radians, as Math.tan() expects an input value in radians.

Returning a random integer between two bounds

This can be achieved with a combination of Math.random() and Math.floor() :

function random(min, max)  const num = Math.floor(Math.random() * (max - min + 1)) + min; return num; > random(1, 10); 

Specifications

Browser compatibility

BCD tables only load in the browser

See also

Found a content problem with this page?

This page was last modified on Apr 26, 2023 by MDN contributors.

Your blueprint for a better internet.

MDN

Support

Our communities

Developers

Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.

Источник

Function random #

math.js

Return a random number larger or equal to min and smaller than max using a uniform distribution.

Syntax #

math.random() // generate a random number between 0 and 1 math.random(max) // generate a random number between 0 and max math.random(min, max) // generate a random number between min and max math.random(size) // generate a matrix with random numbers between 0 and 1 math.random(size, max) // generate a matrix with random numbers between 0 and max math.random(size, min, max) // generate a matrix with random numbers between min and max 

Parameters #

Parameter Type Description
size Array | Matrix If provided, an array or matrix with given size and filled with random values is returned
min number Minimum boundary for the random value, included
max number Maximum boundary for the random value, excluded

Returns #

Throws #

Examples #

math.random() // returns a random number between 0 and 1 math.random(100) // returns a random number between 0 and 100 math.random(30, 40) // returns a random number between 30 and 40 math.random([2, 3]) // returns a 2x3 matrix with random numbers between 0 and 1 

Источник

Math.random()

The Math.random() static method returns a floating-point, pseudo-random number that’s greater than or equal to 0 and less than 1, with approximately uniform distribution over that range — which you can then scale to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.

Note: Math.random() does not provide cryptographically secure random numbers. Do not use them for anything related to security. Use the Web Crypto API instead, and more precisely the window.crypto.getRandomValues() method.

Try it

Syntax

Return value

A floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive).

Examples

Note that as numbers in JavaScript are IEEE 754 floating point numbers with round-to-nearest-even behavior, the ranges claimed for the functions below (excluding the one for Math.random() itself) aren’t exact. If extremely large bounds are chosen (2 53 or higher), it’s possible in extremely rare cases to reach the usually-excluded upper bound.

Getting a random number between 0 (inclusive) and 1 (exclusive)

function getRandom()  return Math.random(); > 

Getting a random number between two values

This example returns a random number between the specified values. The returned value is no lower than (and may possibly equal) min , and is less than (and not equal) max .

function getRandomArbitrary(min, max)  return Math.random() * (max - min) + min; > 

Getting a random integer between two values

This example returns a random integer between the specified values. The value is no lower than min (or the next integer greater than min if min isn’t an integer), and is less than (but not equal to) max .

function getRandomInt(min, max)  min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min) + min); // The maximum is exclusive and the minimum is inclusive > 

Note: It might be tempting to use Math.round() to accomplish that, but doing so would cause your random numbers to follow a non-uniform distribution, which may not be acceptable for your needs.

Getting a random integer between two values, inclusive

While the getRandomInt() function above is inclusive at the minimum, it’s exclusive at the maximum. What if you need the results to be inclusive at both the minimum and the maximum? The getRandomIntInclusive() function below accomplishes that.

function getRandomIntInclusive(min, max)  min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1) + min); // The maximum is inclusive and the minimum is inclusive > 

Specifications

Browser compatibility

BCD tables only load in the browser

See also

Found a content problem with this page?

This page was last modified on Mar 28, 2023 by MDN contributors.

Your blueprint for a better internet.

Источник

Читайте также:  Recyclerview kotlin android пример
Оцените статью