O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Generating Random Numbers

// Random number between 0 and 1 Math.random(); //Random number between 1 and 10 Math.floor(Math.random() * 10) + 1; //Random number between -10 and 10 Math.floor(Math.random() * 20) - 10;

The Math.random() function generates a random float value between 0 and 1. To convert that to an integer, multiply the results of Math.random() by the maximum value you want for the integer and then use Math.floor() to round it down.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required