Sie sind auf Seite 1von 3

25/08/2020

JavaScript - Math.random() method example


1 contributors 2 contributions 0 discussions
10 points
Created by:   JustMike  3411
The  Math.random()  function returns floating-point, pseudo-random number between range [0,1), 0 (inclusive) and 1
(exclusive). Based on this function we are able to get random number in range as we can see on below examples.
Simple usage example:
1 console.log( Math.random() ); // 0.9100486004606172
2 console.log( Math.random() ); // 0.8630710741089413
3 console.log( Math.random() ); // 0.8052253695967542
4
5 function randomInt(min, max) {
6 return min + Math.floor((max - min) * Math.random());
7 }
8
9 function randomFloat(min, max) {
10 return min + (max - min) * Math.random();
11 }
12 https://dirask.com/posts/x1R6G1
13 console.log( randomInt( 10, 20) ); // 12
14 console.log( randomInt(-10, 10) ); // -4
15
16 console.log( randomFloat( 10, 20) ); // 14.514897223860018
17 console.log( randomFloat(-10, 10) ); // -6.645075993092653

Run Auto running Reset

1. Documentation Edit
Syntax Math.random()

Parameters This method does not take any arguments.


Result Float  number  value (primitive value).
random  is static method that returns random float number from range  <0, 1)  - inclusive 0 and
Description exclusive 1.

2. Custom random method examples Edit


2.1. Random float in range example (exclusive max value) Edit
This example shows how to random number with exclusive max.  randomzeFloat()  method is overridden in following
ways:
randomzeFloat()  - generates numbers in range  0  to  Number.MAX_VALUE  (exclusive),
randomzeFloat(max)  - generate numbers in range  0  to  max  (exclusive) -  max  value must be positive,
randomzeFloat(min, max)  - generates numbers in range  min  to  max  (exclusive).

1 /*
2 inclusive min (result can be equal to min value)
3 exclusive max (result will not be to max value)
4 */
5 function randomizeFloat(min, max) {
6 if (max == null) {
7 if (min <= 0) {
8 throw new Error('Max value must be positive.');
9 }

https://dirask.com/posts/JavaScript-Math-random-method-example-x1R6G1 1/3
25/08/2020
10
11 max = (min == null ? Number.MAX_VALUE : min);
12 min = 0.0;
13 }
14
15 if (min >= max) {
16 throw new Error("Incorrect arguments.");
17 }
18
19 return min + (max - min) * Math.random();
20 }
21
22 // Example:
23
24 console.log(randomizeFloat()); // 1.67319916301163e+308
25 console.log(randomizeFloat(5)); // 2.7593705936801918
26 console.log(randomizeFloat(10, 80)); // 37.54521514384005
27 console.log(randomizeFloat(-50, 50)); // -30.632843429520975

Run Auto running Reset


2.2. Random float in range example (inclusive max value) Edit
1 // Generates values from <0, 1>
2 function randomizeValue() {
3 var value = (1 + 10E-16) * Math.random();
4
5 if (value > 1.0) {
6 return 1.0;
7 }
8
9 return value;
10 }
11
12 /*
13 inclusive min (result can be equal to min value)
14 inclusive max (result will not be to max value)
15 */
16 function randomizeFloat(min, max) {
17 if(max == null) {
18 max = (min == null ? Number.MAX_VALUE : min);
19 min = 0.0;
20 }
21
22 if(min >= max) {
23 throw new Error("Incorrect arguments.");
24 }
25
26 return min + (max - min) * randomizeValue();
27 }
28
29 // Example:
30
31 console.log(randomizeFloat()); // 1.1960373039711962e+308
32 console.log(randomizeFloat(5)); // 0.7663988388633522
33 console.log(randomizeFloat(10, 80)); // 67.81113931017913
34 console.log(randomizeFloat(-50, 50)); // -13.713816892801674

Run Auto running Reset


2.3. Random integer in range example (exclusive max value) Edit
1 /*
2 inclusive min (result can be equal to min value)
3 exclusive max (result will not be to max value)
4 */
https://dirask.com/posts/JavaScript-Math-random-method-example-x1R6G1 2/3
25/08/2020
5 function randomizeInteger(min, max) {
6 if(max == null) {
7 max = (min == null ? Number.MAX_SAFE_INTEGER : min);
8 min = 0;
9 }
10
11 min = Math.ceil(min); // inclusive min
12 max = Math.floor(max); // exclusive max
13
14 if(min > max - 1) {
15 throw new Error("Incorrect arguments.");
16 }
17
18 return min + Math.floor((max - min) * Math.random());
19 }
20
21 // Example:
22
23 console.log(randomizeInteger()); // 5547382624322139
24 console.log(randomizeInteger(5)); // 3
25 console.log(randomizeInteger(10, 80)); // 62
26 console.log(randomizeInteger(-50, 50)); // -8

Run Auto running Reset


2.4. Random integer in range example (inclusive max value) Edit
1 /*
2 inclusive min (result can be equal to min value)
3 inclusive max (result will not be to max value)
4 */
5 function randomizeInteger(min, max) {
6 if(max == null) {
7 max = (min == null ? Number.MAX_SAFE_INTEGER : min);
8 min = 0;
9 }
10
11 min = Math.ceil(min); // inclusive min
12 max = Math.floor(max); // exclusive max
13
14 if(min > max - 1) {
15 throw new Error("Incorrect arguments.");
16 }
17
18 return min + Math.floor((max - min + 1) * Math.random());
19 }
20
21 // Example:
22
23 console.log(randomizeInteger()); // 5918572174489812
24 console.log(randomizeInteger(5)); // 5
25 console.log(randomizeInteger(10, 80)); // 60
26 console.log(randomizeInteger(-50, 50)); // -15

Run Auto running Reset


References Edit
. Random number generation - Wikipedia
. Pseudorandom number generator - Wikipedia
. List of random number generators - Wikipedia

https://dirask.com/posts/JavaScript-Math-random-method-example-x1R6G1 3/3

Das könnte Ihnen auch gefallen