AlfieWebDevTips Homefor new and experienced developers
[Home Home] [Web Development Web Development ] [Code Generators Code Generators ] [Resources Resources ] [Contact Us Contact ] |WebDevTips Privacy Privacy |
Print This Page

You are here : Home - Resources - FAQ - Javascript Q7

FAQ - Javascript Q7



Q7:How do I generate a random number within a certain range?

A:There are lots of ways to generate random numbers but here's the way I like doing it :)

Just change the values min_num and max_num to whatever range you need.

<script language="JavaScript" type="text/javascript"> 
<!-- 
var min_num = 20; 
var max_num = 100; 
var diff = max_num-min_num+1 ; 
rnd_number=Math.floor(Math.random()*diff + min_num); 
// --> 
</script>


View 10 random numbers between 20 and 100


back to faq




Google
 

[back to top of page]   

[labelled with icra -  - this link will open in a new window] [RSS Content]

[Copyright © WebDevTips]