Generate a random integer between 2 and 10 : random « Math « JavaScript Tutorial






<html>
<head>
<title>Example</title>
</head>
<body>
<script type="text/javascript">
function selectFrom(startValue, endValue) {
    var iChoices = endValue - startValue + 1;
    return Math.floor(Math.random() * iChoices + startValue);
}


var iNum = selectFrom(2, 10);

alert(iNum);
</script>

</body>
</html>








9.21.random
9.21.1.Math.random()
9.21.2.Use Math.random to generate random number
9.21.3.Math.round a random number
9.21.4.Generate a random integer between 2 and 10
9.21.5.Generate random URL
9.21.6.Generate random numbes in a range