JavaScript Kit General JavaScript Tutorials
http://www.javascriptkit.com/javaindex.shtml

 

Formatting numbers for decimals and significant digits
Need to display a number in currency format? How about a number that is x digits in length? See how to easily, using two new methods of JavaScript 1.5+.

Generating weighed random numbers in JavaScript
See the technique for adding weight to random numbers, so some show up more often than others.

Using the navigator object to detect client's browser
In today's haphazard browser market, browser detection is as important as ever in our scripts. Learn about using the navigator object for the task.

The Math object of JavaScript
Looking to pull off a few mathematical operations using JavaScript? You'll need to arm yourself with the Math object.

Number rounding in JavaScript
Round up your brain cells as we explore number rounding in JavaScript!

Generating a random number in JavaScript
A random number is useful- if not required- in the creation of certain popular JS applications, such as a dice, random image script, or random link generator. Learn how to generate a random number in JavaScript.

Strings (text)

Math

Understanding arrays and loops

 

 

Introductory Tutorials

 

Interacting with images

Interacting with forms

Browser detection New

Other tutorials...

 

Generating a random number in JavaScript

A popular question on the JK Help Forum is how to generate a random number in JavaScript. A random number is useful- if not required- in the creation of certain popular JS applications, such as a dice, random image script, or random link generator. In this JavaScript article, we'll learn how to output a random number that falls within the range 1 to x, where x is any integer >1.

The results may be unpredictable, but the technique to getting them certainly is not. To generate a random number in JavaScript, simply use the following code:

var randomnumber=Math.floor(Math.random()*11)

where 11 dictates that the random number will fall between 0-10. To increase the range to, say, 100, simply change 11 to 101 instead.

Some of you may be curious as to why Math.floor(), instead of Math.round(), is used in the above code. While both successfully round off its containing parameter to an integer within the designated range, Math.floor does so more "evenly", so the resulting integer isn't lopsided towards either end of the number spectrum. In other words, a more random number is returned using Math.floor(). By the way, the Random Motivational script above (http://javascriptkit.com/script/script2/motivatequotes.shtml) uses just the aforementioned code to randomly display a quote.