js 随机函数

JavaScript012

js 随机函数,第1张

在写js中,我们经常遇见 js 随机函数,总结一下最近写验证码的时候  用到过的js 随机函数

Math.random()结果为0-1间的随机数(包括0,不包括1)

Math.floor( n )参数为Number 类型 ,函数结果 为num 的整数部分

Math.round( n ) ; 参数为Number 类型。函数结果 为num 四舍五入部分

Math.ceil( n )参数为Number类型。 函数结果为大于等于n的整数

Math.ceil(Math.random()*10) : 主要获取1到10的随机整数,取0的几率极小。

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

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

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

例子:

    1. 实现一个 随机 汉字

         randomChi(){   // 随机生成汉字

                return `\\u${Math.round(Math.random() * 20901 + 19968 ).toString(16)}`

           }

    2. 实现一个n - m 的整数 ( 参照于 http://www.jb51.net/article/56019.htm )

        生成 n-m , 包含n 但不包含 m的整数

            第一步算出 m -n的值,假设等于 w 

            第二步 Math.random( )  * w

            第三步 Math.random() * w +n

            第四步 parseInt( Math.random() * w +n, 10)

         生成一个 n-m ,包含 m 但不包含 n的整数

            第一步算出 m-n的值,假设等于w

            第二步Math.random()*w

            第三步Math.random()*w+n

            第四步Math.floor(Math.random()*w+n) + 1

         生成一个不包含 n -m 但都不包含 n  和 m

             第一步算出 m-n-2的值,假设等于w

              第二步Math.random()*w

              第三步Math.random()*w+n +1

               第四步Math.round(Math.random()*w+n+1) 或者 Math.ceil(Math.random()*w+n+1)

            生成n-m,包含n和m的随机数:

                第一步算出 m-n的值,假设等于w

                第二步Math.random()*w

                第三步Math.random()*w+n

                第四步Math.round(Math.random()*w+n) 或者 Math.ceil(Math.random()*w+n)

             实例: 生成800-1500的随机整数,包含800但不包含1500

                1500-800 = 700

                Math.random()*700

                var num = Math.random()*700 + 800

                num = parseInt(num, 10)

更多实例 请查看  https://github.com/Mrangmaomao

<html> 

  <head> 

    <title>纯字验证码</title> 

    <meta http-equiv='content-type' content='text/htmlcharset=utf-8'/> 

    <script type='text/javascript' src='jquery-1.7.2.js'></script> 

    <script type='text/javascript'> 

    var code  //在全局定义验证码  

       

    function createCode(){ 

       code = ""  

       var codeLength = 4//验证码的长度  

       var checkCode = document.getElementById("code")  

       var random = new Array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R',  

       'S','T','U','V','W','X','Y','Z')//随机数  

       for(var i = 0 i < codeLength i++) {//循环操作  

        var index = Math.floor(Math.random()*36)//取得随机数的索引(0~35)  

        code += random[index]//根据索引取得随机数加到code上  

      }  

      checkCode.value = code//把code值赋给验证码  

    } 

    //校验验证码  

    function validate(){  

      var inputCode = document.getElementById("input").value.toUpperCase() //取得输入的验证码并转化为大写     

      if(inputCode.length <= 0) { //若输入的验证码长度为0  

        alert("请输入验证码!") //则弹出请输入验证码  

      }else if(inputCode != code ) { //若输入的验证码与产生的验证码不一致时  

        alert("验证码输入错误!@_@") //则弹出验证码输入错误  

        createCode()//刷新验证码  

        document.getElementById("input").value = ""//清空文本框  

      }else { //输入正确时  

        alert("合格!^-^") 

      } 

    } 

    </script> 

    <style type='text/css'> 

    #code{ 

      font-family:Arial,宋体 

      font-style:italic 

      color:green 

      border:0 

      padding:2px 3px 

      letter-spacing:3px 

      font-weight:bolder 

    } 

    </style> 

  </head> 

  <body onload='createCode()'> 

    <div>验证码:  

      <input type = "text" id = "input"/>  

      <input type="button" id="code" onclick="createCode()" style="width:60px" title='点击更换验证码' /> 

      <input type = "button" value = "验证" onclick = "validate()"/> 

    </div>  

  </body> 

</html>

首先新建一个测试网页,里面没有什么多余的内容。

在网页中写一个label input框 一个按钮。

预览一下效果。要实现的功能就是,点击一下获取验证码按钮,生成6位随机数写入到input框中。

在网页中添加必要的script便签。为按钮添加单击事件。

写产生验证码的js。只有简单的3行,就不过多的解释。