Languages
[Edit]
EN

JavaScript - Math.random() method example

13 points
Created by:
Aisha
418

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:

Alternative solution: 

 


1. Documentation

Edit
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

Edit

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

Edit

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).

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

Edit

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

Edit

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

Edit

 

References

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

Post thumbnail

Edit

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
1
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.

JavaScript - Math documentation (EN)

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