| |
|
Random number Math function |
Random numbers are required in different applications like game, dice and many other applications. We can use JavaScript math function random() to generate random number between 0 and 1. We will use this function to generate random numbers between other numbers also. Here is the syntax
math.random(number);
The return value from this function is random number between 0 and 1. As we have to remove the decimal part to get random numbers between 0 to 10.
We will multiply the random number returned by math.random() by 11 and then take the floor value of it to get the random number. We have used floor value as this function returns the lower integer part only. If we use round function then the value will be adjusted to nearest integer so floor function gives more accurate random number.
Here is a random number generator, go on clicking the button to get random numbers.
Here is the total code of this demo.
<html>
<head>
<title>(Type a title for your page here)</title>
<script type="text/javascript">
function generate(){
var my_num=Math.random();
document.f1.t1.value=(my_num*11);
document.f1.t2.value=Math.floor(my_num*11);
}
</script>
</head>
<body >
<form name=f1>
Random number after multiplying with 11
<input type=text name=t1>
<br>After taking the floor value
<input type=text name=t2>
<input type=button value='Display' onClick='generate()';>
</form>
</body>
</html>
You can read the php random number generator and
ASP random number generator
| |
| Subscribe |
|
Submit your email address and receive
article and product notifications. Your email is safe with us.
|
|
|
|
| JavaScript Tutorials |
| Math Functions |
|
|
|
| Popular Tutorials |
|
| Drop down list |
| Timer function |
| JavaScript Tutorials |
|
| String |
| Array |
| Date & Time |
| Form Validation |
| Event Handling |
| Math Functions |
| JavaScript Forum |
| Subscribe |
|
Submit your email address and receive
article and product notifications. Your email is safe with us.
|
|
|
|