|
|
Generating Random Numbers in JavaScriptTo generate random numbers with JavaScript we employ the random() method of the Math object. It returns a floating-point number between 0.0 and 1.0. var rand_no = Math.random(); alert(rand_no); In the code above, the random() method returns the number which we store in the variable rand_no and display through alert(). Click to display a random number. Now that we have our randomly generated number with JavaScript, let us see how we can use it in some of our applications. How to use JavaScript random numberSuppose you want a random number between 1 and 100. How do you get it from the long decimal number thrown up by the random() method? The first step is to multiply the long decimal random number generated by the random() method with 100. var rand_no = Math.random(); rand_no = rand_no * 100; alert(rand_no); Click to get see a set of 10 random numbers. The code above will give us a random number between 0 and 10. All numbers will be a little more than 0 and a little les than 100 - and they still have the long tail of numbers after the decimal. The next step involves another method of the JavaScript Math() object called ceil(). JavaScript ceil() methodThe JavaScript ceil() rounds a decimal number to the next higher integer. Thus, Math.ceil(2.456) //gives 3 Math.ceil(46.9) //gives 47 Math.ceil(0.0006) // gives 1 To remove numbers after the decimal and provide us with an integer between 1 and 100, we will pass the random number generated by random() to ceil(). var rand_no = Math.random(); rand_no = rand_no * 100; rand_no = Math.ceil(rand_no); alert(rand_no); Click to generate a random number between 1 and 100. var rand_no = Math.ceil(100*Math.random()) alert(rand_no); JavaScript generates random numbers based on a formula. In a sense this is not random because if you know the formula, you know which number will come up next. However, this works fine for the web applications you will develop. What if you want random numbers between 0 and 10? Math.floor() methodThe floor() rounds a number down to the lower integer. Thus: Math.floor(2.456) //gives 2 Math.floor(46.9) //gives 46 Math.floor(0.0006) // gives 0 Math.floor(1.0006) // gives 1 Math.floor(0.932) // gives 0 But this throws up another problem. Number between 0.9 and 1.0 will all be rounded down (after multiplying with 10) to 9! var rand_no = Math.floor(11*Math.random()) alert(rand_no); List of 20 random number between 0 and 10. Random number from a given rangeTo generate random numbers from a given range, follow the steps below:
var rand_no = Math.floor((10-4)*Math.random()) + 5; alert(rand_no); The code above generates a random number between 5 and 10. Just to prove that this works beautifully click to get a list of 20 random number between 5 and 10
Page contents: Generating random numbers in javascript - javascript random numbers calculations and methods
Page URL: http://www.webdevelopersnotes.com/tutorials/javascript/ generating_random_numbers_javascript.php3
|
|