Javascript generate a random number using Math.random
Posted by in Javascript February 24, 2011 Leave a comment

A random number is useful in the creation of JavaScript applications such as random image script, random link generator.

To generate a random number between 1 and 10, simply use code example below:

<script type="text/javascript">
$(document).ready(function()
{
	var rand_no = Math.ceil(10*Math.random());
	alert(rand_no);
 
});
</script>

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

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at hoan@4rapiddev.com