js取指定范围随机值【原】

时间:2023-01-24 15:06:02

js取指定范围随机值

<HTML>
<HEAD>
<meta http-equiv="content-Type"content="text/html;nameset=utf-8">
<TITLE>取指定范围随机数</TITLE> <script type="text/javascript">
var names = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']; /* min<=取值<=max*/
function GetRandomNum(min,max)
{
var Range = max - min;
var Rand = Math.random();
return (min + Math.round(Rand * Range));
} function getResult(){
var length = names.length-1;
var index = GetRandomNum(0,length);
alert(names[index]);
} </script>
</HEAD>
<BODY>
<button onclick="getResult()"> 随机取数</button>
</BODY>
</HTML>