JavaScript random numbers examples

Generating random numbers is a common thing in programming. Here is some examples in JavaScript:

//Basic Random
Math.random();
//From 0 to specific number
Math.random() * 10;
//Removing decimals
Math.floor(Math.random() * 10);
//Random between 5 and 10
Math.floor(Math.random() * 5) + 5;
//Random between 5 and 10 (included 10)
Math.floor(Math.random() * 5) + 6;

Watch this demonstration video:

Here is live preview from Code Pen:

See the Pen JavaScript Random Number by Muhammad Habibie (@habibieamrullah) on CodePen.

loading...

Leave a Reply

Your email address will not be published. Required fields are marked *