这篇文章主要介绍了基于 javascript 实现彩票随机数生成的相关资料, 需要的朋友可以参考下
Javascript 是一种由 Netscape 的 LiveScript 发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如 Perl,遗留的速度问题,为客户提供更流畅的浏览效果。
效果图:
具体代码:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>
- Math.random方法彩票随机数的生成
- </title>
- </head>
- <body>
- <!-- 设置样式 -->
- <input type="text" id="text">
- <button id="btnGo">
- 开始
- </button>
- <button id="btnStop">
- 获取随机数组
- </button>
- <script type="text/javascript">
- //获取节点
- var btnGo = document.getElementById("btnGo");
- var btnStop = document.getElementById("btnStop");
- var text = document.getElementById("text");
- //定义生成最小到最大值的随机函数
- function rand(min, max) {
- return parseInt(Math.random() * (max - min + 1) + min);
- }
- function start(min, max, length) {
- //定义空数组
- var arr = [];
- while (arr.length < length) {
- //生成一个随机数prem
- var prem = rand(min, max);
- //判断生成的随机数prem是否在数组arr里,果然不在,就将这个随机数插入到数组里,如果在,执行下一次循环
- if (arr.indexOf(prem) == -1) {
- arr.push(prem);
- }
- }
- //返回数组arr
- return arr;
- }
- var timer = 0;
- //单击开始按钮生成随机数组
- btnGo.onclick = function() {
- //清除
- clearInterval(timer);
- timer = setInterval(function() {
- text.value = start(1, 33, 7);
- },
- 50)
- }
- //单击停止按钮获取一组随机数
- btnStop.onclick = function() {
- clearInterval(timer);
- }
- </script>
- </body>
- </html>
以上就是本文的详细内容,希望对大家的学习 javascript 程序设计有所帮助。
来源: http://www.phperz.com/article/17/0406/267639.html