XIN'BOLG

导航

4月6日--求指定范围的随机数

  转换随机数为指定范围的整数
  1、Math.floor(Math.random()*(MAX-MIN+1))+MIN

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Math的常用方法3</title>
    <script type="text/javascript">
        var n=Math.random();//0-1
        console.log(n);
        //转换随机数为指定范围的整数
        //Math.floor(Math.random()*(MAX-MIN+1))+MIN
        //[0,100] 
        // n=Math.floor(Math.random()*101);
        //[4,57]
        n=Math.floor(Math.random()*54)+4;
        console.log(n);
    </script>
</head>
<body>
    
</body>
</html>

 

posted on 2017-04-06 15:01  XIN'BOLG  阅读(114)  评论(0编辑  收藏  举报