javascript生成随机四位数

在网页编程中,经常会用到随机数,像生成订单号这些都会用到。

今天小编分享的是使用javascript生成随机四位数,以下为实例代码。

    <input type="button" id="btn" value="点击生成随机四位数" />
    <span id="num"></span>
    <script>
    function rand(min,max) {
        return Math.floor(Math.random()*(max-min))+min;
    }
    var btn=document.getElementById("btn");
    btn.onclick=function(){
        var randnum=rand(1000,9999);
        document.getElementById("num").innerHTML=randnum;
    }
    </script>

当然你还可以修改rand里面的数字,达到自己想要的效果。

原文转自三体教程http://www.santii.com/article/133.html

posted @ 2017-11-07 09:29  名字有点长  阅读(11869)  评论(0编辑  收藏  举报