zoukankan      html  css  js  c++  java
  • JavaScript Match

    JavaScript Match

      版权声明:未经授权,严禁转载!  


     随机数

    // 随机数  Math.random() 随机生成一个大于等于0且小于1的小数。
    // 0>= r < 1 [0,1)  0~0.99999·····
    console.log(Math.random());
    console.log(Math.random());
    console.log(Math.random());
    console.log(Math.random());
    
    // 计算概率
    console.log(Math.random()<0.5?"❀":"字");
    
    //谁去拿买卖
    var r=Math.random();
    console.log(
        r<0.33?"张":
            r<0.66?"王":
                "乔"
    );

      

    // 取[0,10)之间一个数。
    console.log(Math.random()*10);
    // 取[0,5)之间一个数。
    console.log(Math.random()*5);
    
    
    // 取[0,10)之间一个整数。
    console.log(
        // Math.random()*max
        Math.floor(Math.random()*10)
    );// math.floor() 取整
    
    // 取[0,5)之间一个整数。
    console.log(parseInt(Math.random()*5)); // math.floor() 取整
    
    
    // 取[1,10] 之间一个整数。
    console.log(Math.floor(Math.random()*10)+1); // math.floor() 取整
    // 取[1,5]之间一个整数。
    console.log(parseInt(Math.random()*5)+1); // math.floor() 取整
    
    
    // 取 [22,35] 之间的随机整数
    console.log(
        // Math.floor(Math.random()*(max-min+1))+min
        Math.floor(Math.random()*14)+22
    );

        

    案例:

      随机生成一个四个字符的验证码,可包含大小写字母和数字

      大写字母的 Unicode 为65-90,小写字母的 Unicode 为 97-122.

        var arr=[];
        //将所有可用字符保存到数组中
        for(var i=0;i<10;i++){//0-9
          arr.push(i);
        }
        for(var i=65;i<=90;i++){//A-Z
          arr.push(String.fromCharCode(i));
        }
        for(var i=97;i<=122;i++){//a-z
          arr.push(String.fromCharCode(i));
        }
    
        //生成验证码
        var text="";
        for(var i=0;i<4;i++){
          text+=arr[parseInt(Math.random()*arr.length)];//0-arr.length-1
        }
        console.log(text);

        

  • 相关阅读:
    如何利用FineBI做财务分析
    mysqlbinlog 读取多个文件
    Chapter 13. Miscellaneous PerlTk Methods PerlTk 方法杂项:
    跨越多台haproxy 跳转
    haproxy redirect prefix
    大数据决策领跑零售业
    大数据决策领跑零售业
    perl 实现微信简版
    perl 调用按钮输出到文本框
    Chapter 11. Frame, MainWindow, and Toplevel Widgets 框架,主窗体,顶级部件
  • 原文地址:https://www.cnblogs.com/wjw1014/p/8980718.html
Copyright © 2011-2022 走看看