Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JavaScript ---- Math mathematical objects (universal random number formula)


May 30, 2021 Article blog


Table of contents


Concept: is a mathematical object, not a constructor, it has mathematical constants and function properties and methods

  • Math.PI - circumference;
  • Math.floor() - rounding down;
  • Math.ceil() - rounding up;
  • Math.round() - rounding;
  • Math.abs() - absolute value;
  • Math.max()/Math.min() - find the maximum and minimum values;
  • Math.random() - Gets a random value with a range of 0,1) and can randomly return a fraction, with the value range (0,1) open left and right.

Universal formula

Gets random numbers in a range

Value : Math.floor (Math.random() s total number of possible values plus the first possible value)

Example of a small case code

<script>

Var Arr = []; // Create an empty number set of random numbers

For (var i = 0; i <5; i ++) {// loop print 5 random numbers

Arr [i] = math.round (math.random () * 26 + 1); // value = random number formula

FOR (var j = 0; j <i; j ++) {// loop 5 random numbers

IF (arr [i] == arr [j]) {// is compared

I -; // If equally, the number of times is not recorded because the number of random numbers in the loop can be regenerated.

}

}

}

Console.log (arr); // 5 random numbers to the console

</script>