Languages
[Edit]
EN

JavaScript - Math.random() method example

10 points
Created by:
Aisha
328

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 a random number in range as we can see in the below examples.

Quick solutions:

// ONLINE-RUNNER:browser;

console.log( Math.random() ); // 0.9100486004606172
console.log( Math.random() ); // 0.8630710741089413
console.log( Math.random() ); // 0.8052253695967542
// ONLINE-RUNNER:browser;

function randomInt(min, max) {
	return min + Math.floor((max - min) * Math.random());
}

console.log( randomInt( 10, 20) ); // 12
console.log( randomInt(-10, 10) ); // -4
// ONLINE-RUNNER:browser;

function randomFloat(min, max) {
    return min + (max - min) * Math.random();
}

console.log( randomFloat( 10, 20) ); // 14.514897223860018
console.log( randomFloat(-10, 10) ); // -6.645075993092653

 


1. Documentation

SyntaxMath.random()
ParametersThis method does not take any arguments.
ResultFloat number value (primitive value).
Descriptionrandom is a static method that returns a random float number from the range <0, 1) - inclusive 0 and exclusive 1.

2. Custom random method examples

2.1. Random float in range example (exclusive max value)

This example shows how to random numbers with exclusive max. randomizeFloat() method is overridden in the following ways:

  • randomizeFloat() - generates numbers in a range from 0 to Number.MAX_VALUE (exclusive),
  • randomizeFloat(max) - generate numbers in a range from 0 to max (exclusive) - max value must be positive,
  • randomizeFloat(min, max) - generates numbers in a range from min to max (exclusive).
// ONLINE-RUNNER:browser;

/*
	inclusive min (result can be equal to min value)
    exclusive max (result won't be equal to max value)
*/
function randomizeFloat(min, max) {
  	if (max == null) {
  	  	if (min <= 0) {
  	  	  	throw new Error('Max value must be positive.');
  	  	}

    	max = (min == null ? Number.MAX_VALUE : min);
      	min = 0.0;
    }
  
  	if (min >= max) {
    	throw new Error("Incorrect arguments.");
    }

    return min + (max - min) * Math.random();
}

// Example:

console.log(randomizeFloat()); // 1.67319916301163e+308
console.log(randomizeFloat(5)); // 2.7593705936801918
console.log(randomizeFloat(10, 80)); // 37.54521514384005
console.log(randomizeFloat(-50, 50)); // -30.632843429520975

2.2. Random float in range example (inclusive max value)

// ONLINE-RUNNER:browser;

// Generates values from <0, 1>
function randomizeValue() {
	var value = (1 + 10E-16) * Math.random();
  
  	if (value > 1.0) {
    	return 1.0;
    }
  
  	return value;
}

/*
	inclusive min (result can be equal to min value)
    inclusive max (result can be equal to min value)
*/
function randomizeFloat(min, max) {
  	if(max == null) {
    	max = (min == null ? Number.MAX_VALUE : min);
      	min = 0.0;
    }
  
  	if(min >= max) {
    	throw new Error("Incorrect arguments.");
    }

    return min + (max - min) * randomizeValue();
}

// Example:

console.log(randomizeFloat()); // 1.1960373039711962e+308
console.log(randomizeFloat(5)); // 0.7663988388633522
console.log(randomizeFloat(10, 80)); // 67.81113931017913
console.log(randomizeFloat(-50, 50)); // -13.713816892801674

2.3. Random integer in range example (exclusive max value)

// ONLINE-RUNNER:browser;

/*
	inclusive min (result can be equal to min value)
    exclusive max (result won't be equal to max value)
*/
function randomizeInteger(min, max) {
  	if(max == null) {
    	max = (min == null ? Number.MAX_SAFE_INTEGER : min);
      	min = 0;
    }

    min = Math.ceil(min);  // inclusive min
    max = Math.floor(max); // exclusive max

  	if(min > max - 1) {
    	throw new Error("Incorrect arguments.");
    }

    return min + Math.floor((max - min) * Math.random());
}

// Example:

console.log(randomizeInteger()); // 5547382624322139
console.log(randomizeInteger(5)); // 3
console.log(randomizeInteger(10, 80)); // 62
console.log(randomizeInteger(-50, 50)); // -8

2.4. Random integer in range example (inclusive max value)

// ONLINE-RUNNER:browser;

/*
	inclusive min (result can be equal to min value)
    inclusive max (result can be equal to min value)
*/
function randomizeInteger(min, max) {
  	if(max == null) {
    	max = (min == null ? Number.MAX_SAFE_INTEGER : min);
      	min = 0;
    }

    min = Math.ceil(min);  // inclusive min
    max = Math.floor(max); // exclusive max

  	if(min > max - 1) {
    	throw new Error("Incorrect arguments.");
    }

    return min + Math.floor((max - min + 1) * Math.random());
}

// Example:

console.log(randomizeInteger()); // 5918572174489812
console.log(randomizeInteger(5)); // 5
console.log(randomizeInteger(10, 80)); // 60
console.log(randomizeInteger(-50, 50)); // -15

References

  1. Random number generation - Wikipedia
  2. Pseudorandom number generator - Wikipedia
  3. List of random number generators - Wikipedia

Post thumbnail

Post thumbnail - JavaScript - Math.random() method example - link https://dirask.com/q/x1R6G1

Alternative titles

  1. JavaScript - Math.random() documentation with examples
  2. js - Math.random() method documentation with examples
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Cross technology - Math.random()

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

â€ïžđŸ’» 🙂

Join