zoukankan      html  css  js  c++  java
  • 生成随机数 ,随机密码

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

    函数功能:生成[n,m]的随机整数。

    在js生成验证码或者随机选中一个选项时很有用

    //生成从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; 
        } 
    } 

    二 、生成n位数的随机密码

    function randomPassword(length) {
      length = Number(length)
      // Limit length
      if (length < 6) {
        length = 6
      } else if (length > 16) {
        length = 16
      }
      let passwordArray = ['ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz', '1234567890', '!@#$%&*.'];
      var password = [];
      let n = 0;
      for (let i = 0; i < length; i++) {
        // If password length less than 9, all value random
        if ( password.length < (length - 4) ) {
          // Get random passwordArray index
          let arrayRandom = Math.floor(Math.random() * 4);
          // Get password array value
          let passwordItem = passwordArray[arrayRandom];
          // Get password array value random index
          // Get random real value
          let item = passwordItem[Math.floor(Math.random() * passwordItem.length)];
          password.push(item);
        } else {
          // If password large then 9, lastest 4 password will push in according to the random password index
          // Get the array values sequentially
          let newItem = passwordArray[n];
          let lastItem = newItem[Math.floor(Math.random() * newItem.length)];
          // Get array splice index
          let spliceIndex = Math.floor(Math.random() * password.length);
          password.splice(spliceIndex, 0, lastItem);
          n++
        }
      }
      return password.join("");
    }
  • 相关阅读:
    MySQL主键和外键使用及说明
    SQLAlchemy
    路飞学城购买流程API
    路飞学城知识点
    使用rest_framework写api接口的一些注意事项(axios发送ajax请求)
    微信推送功能
    支付宝支付业务
    路飞学城前端Vue
    Python爬虫,用第三方库解决下载网页中文本的问题
    Python爬虫,抓取淘宝商品评论内容
  • 原文地址:https://www.cnblogs.com/gavinjay/p/13968396.html
Copyright © 2011-2022 走看看