[JS]两个常用的取随机整数的函数

复制代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>

 <body onload="test();">
  
 </body>
</html>
<script type="text/javascript">
<!--
    
    function test(){
        for(var i=0;i<10;i++){
            console.log(getRndFromZeroToN(4));
        }
console.log("---------------------------");
        for(var i=0;i<10;i++){
            console.log(getRndBetween(1,4));
        }
    }

//------------------------------------------------------------
// 得到0到N(包括0和N)的随机整数
// 如果要得到数组里面的一个要把数组长度减一
//------------------------------------------------------------
function getRndFromZeroToN(n){
    return Math.floor(Math.random()*(n+1));
}
//------------------------------------------------------------
// 得到lowerLimit到upperlimit(包括端点值)的随机整数
//------------------------------------------------------------
function getRndBetween(lowerLimit,upperLimit){
    return Math.floor(Math.random()*(upperLimit-lowerLimit+1))+lowerLimit;
}
//-->
</script>
复制代码

2019年3月10日16点03分

posted @   逆火狂飙  阅读(493)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东
点击右上角即可分享
微信分享提示