Web Technologies - JAVASCRIPT

Back to Course

Lesson Description

Lession - #227 Javascript Random

Random Method

random.math (>
The Math.random (>
function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1>
, with a roughly uniform distribution throughout that range, which you can adjust to your desired range.

The Math.random(>
method in JavaScript is an effective built-in way for generating random integers. When Math.random(>
is called, it returns a random integer between 0 and 1 that can be anything. The 0 is included, whereas the 1 is not.

Creating a javascript random number i.e,floating point number in the range of 0 to 1

The floating point (decimal>
integer returned by the Math.random(>
method is larger than or equal to 0 and less than (but never equal to>
1. To put it another way, 0=x 1. Consider the following scenario:

; // 0.7069207248635578 console.log(Math.random(>
; // 0.765046694794209 console.log(Math.random(>
; // 0.14069121642698246

(Of course, the numbers that are returned will differ each time.>
For the rest of the examples, this will be assumed; different results will occur on each pass.>
Multiply the value of Math.random(>
by a number to acquire a random number from a bigger range.

Creating a random floating point number between 0 and a given maximum value

Larger numbers, or even integers, are usually required instead of random values between 0 and 1. If you want a random floating point value between 0 and 10, for instance, you may use:

var x = Math.random(>
*10; console.log(x>
; // 4.133793901445541

Within a given range, generate a random floating point number

You could perform something like this if you require a random floating point number that falls between two numbers:

var min = 85.4;
var max = 195.35;

var x = Math.random(>
*(max - min>
+min; console.log(x>
; </script> //190.73463134524513

Making a random integer between 0 and a maximum value

Integers are frequently required. You'll need to utilise several additional Math object methods, such as Math.floor(>
(rounds down to the closest integer>
and Math.ceil(>
, to do this (rounds up to the nearest integer>
For example, if you need to pick one element at random from a ten-element array, you'll require a random integer between 0 and 9, inclusive (remember that arrays are zero indexed>

var x = Math.floor(Math.random(>
; console.log(x>
; // 7 </script>
(Keep in mind that Math.random(>
never returns precisely 1, therefore Math.random(>
*10 never returns exactly 10.>
This indicates that the outcome will always be 9 or less after rounding down.>

Creating a random number between 1 and a maximum

If you require a random number with a minimum of one (say, a random day in January>
, you can use the Math.ceil(>

var x = Math.ceil(Math.random(>
; console.log(x>
; </script> //20

Another option would have been to use the preceding method (Math.floor(>
and multiply it by 1:

var x = Math.floor(Math.random(>
+1; console.log(x>
; </script> //18

Creating a random number within a certain range

Finally, you may want a random integer between two particular values on occasion. For example, suppose you're attempting to choose lottery tickets and you know the lowest and highest numbers:

var min = 1812;
var max = 3223;

var x = Math.floor(Math.random(>
; console.log(x>
; </script> //2736