zoukankan      html  css  js  c++  java
  • js生成[n,m]的随机数 以及实际运用

    Math.ceil();  //向上取整。

    Math.floor();  //向下取整。

    Math.round();  //四舍五入。

    Math.random();  //0.0 ~ 1.0 之间的一个伪随机数。【包含0不包含1】 //比如0.8647578968666494

    Math.ceil(Math.random()*10);      // 获取从1到10的随机整数 ,取0的概率极小。

    Math.round(Math.random());   //可均衡获取0到1的随机整数

    Math.floor(Math.random()*10);  //可均衡获取0到9的随机整数

    Math.round(Math.random()*10);  //基本均衡获取0到10的随机整数,其中获取最小值0和最大值10的几率少一半

    因为结果在0~0.4 为0,0.5到1.4为1...8.5到9.4为9,9.5到9.9为10。所以头尾的分布区间只有其他数字的一半。

    生成[n,m]的随机整数

    //生成从minNum到maxNum的随机数
    function randomNum(minNum,maxNum){ 
        switch(arguments.length){ 
            case 1: 
                return parseInt(Math.random()*minNum+1,10); 
            break; 
            case 2: 
                return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10); 
            break; 
                default: 
                    return 0; 
                break; 
        } 
    }

    Math.random()生成[0,1)的数,所以

    Math.random()*5生成{0,5)的数。

    通常期望得到整数,所以要对得到的结果处理一下。

    parseInt(),Math.floor(),Math.ceil()和Math.round()都可得到整数。

    parseInt()和Math.floor()结果都是向下取整。

    所以Math.random()*5生成的都是[0,4] 的随机整数。

    所以生成[1,max]的随机数,公式如下:

    // max - 期望的最大值
    parseInt(Math.random()*max,10)+1;
    Math.floor(Math.random()*max)+1;
    Math.ceil(Math.random()*max);

    所以生成[0,max]到任意数的随机数,公式如下:

    // max - 期望的最大值
    parseInt(Math.random()*(max+1),10);
    Math.floor(Math.random()*(max+1));

    所以希望生成[min,max]的随机数,公式如下:

    // max - 期望的最大值
    // min - 期望的最小值
    parseInt(Math.random()*(max-min+1)+min,10);
    Math.floor(Math.random()*(max-min+1)+min);
    //实际运用:
               var content = document.getElementById('content');
    
    function getRandom()
    {
    return Math.floor(Math.random()*4);
    
    }
    
    
    var intIndex = getRandom() +1;
    
    
            //for (var i = 0, l = 3; i < l; i++) {
                    var html = "";
                    
                    if(intIndex == 1){
                        html = '<a target="_blank" static="stp=cl"  href="http://mini.eastday.com/a/160805112737568.html?qid=baomihua" title="记者问赵丽颖拍戏有木有生理反应,回答很巧妙"><img src="img/1.jpg" width="189" height="96" alt="记者问赵丽颖拍戏有木有生理反应,回答很巧妙"><div style="margin-top:3px;height: 36px;overflow: hidden;color: #666;font:400 12px/18px 宋体;">记者问赵丽颖拍戏有木有生理反应,回答很巧妙</div></a>'
    
                    }else if(intIndex == 2){
                        html = '<a target="_blank" static="stp=cl"  href="http://mini.eastday.com/a/160805024700594.html?qid=baomihua" title="性感美女桂晶晶火辣写真图片 上演黑色蕾丝诱惑"><img src="img/2.jpg" width="189" height="96" alt="性感美女桂晶晶火辣写真图片 上演黑色蕾丝诱惑"><div style="margin-top:3px;height: 36px;overflow: hidden;color: #666;font:400 12px/18px 宋体;">性感美女桂晶晶火辣写真图片 上演黑色蕾丝诱惑</div></a>'
    
                    }else if(intIndex== 3){
                        html = '<a target="_blank" static="stp=cl"  href="http://mini.eastday.com/a/160805062453900.html?qid=baomihua" title="23岁的她欲夺金牌,感觉里约900万套套不够用了"><img src="img/3.jpg" width="189" height="96" alt="23岁的她欲夺金牌,感觉里约900万套套不够用了"><div style="margin-top:3px;height: 36px;overflow: hidden;color: #666;font:400 12px/18px 宋体;">23岁的她欲夺金牌,感觉里约900万套套不够用了</div></a>'
                    }
                    else
                    {
                        html = '<a target="_blank" static="stp=cl"  href="http://mini.eastday.com/a/160805051603361.html?qid=baomihua" title="超模肯达尔解放身体:我真的不懂有什么大不了的"><img src="img/4.jpg" width="189" height="96" alt="超模肯达尔解放身体:我真的不懂有什么大不了的"><div style="margin-top:3px;height: 36px;overflow: hidden;color: #666;font:400 12px/18px 宋体;">超模肯达尔解放身体:我真的不懂有什么大不了的</div></a>'
                    }
    
    
    
    
                    content.innerHTML = html;
  • 相关阅读:
    快乐的一天从AC开始 | 20210717 | 牛客小白月赛36J
    快乐的一天从AC开始 | 20210717 | P4839
    P7295-[USACO21JAN]Paint by Letters P【平面图欧拉公式】
    泛型
    List集合
    红黑树被定义
    单例模式的双重检查锁模式为什么必须加 volatile?
    什么是 happens-before 规则?
    解决AtomicInteger 在高并发下性能问题
    什么是指令重排序?为什么要重排序?
  • 原文地址:https://www.cnblogs.com/zhangrenjie/p/5884922.html
Copyright © 2011-2022 走看看