search
HomeWeb Front-endJS TutorialExample analysis of using JavaScript to generate lottery numbers

This "Lottery Number Generator" is purely front-end development, involving HTML, JS, and CSS. For the sake of simplicity, all the codes are written into an html file. The advantage of this is that the file can be opened directly with a browser. Of course, in actual work, it is best to put them in different files. The entire area is placed in a large p tag, which appears as a rectangular area, and the display position is controlled through JS; set up a drop-down list, and you can choose to generate 1-5 groups of random numbers. When the button is clicked , you can generate numbers corresponding to the number of groups; the number area is divided into two parts. The upper part is a randomly generated unordered number, and the lower part is a number after sorting these unordered numbers. The number of generated groups is controlled by the drop-down list above. The core of this tool is to generate random numbers and sort the random numbers, both of which are implemented through JS code. Here are some important JS codes.

Generate random numbers: Lotto is divided into front area numbers and rear area numbers. The front area numbers are 5 numbers from 01-35 without repetition, and the rear area numbers are from 01-12 without repetition. Take 2 numbers to form a set of 7-digit numbers. Two arrays are defined here: arr35 stores the 01-35 numbers in the front area, and arr12 stores the 01-12 numbers in the back area. Use the "temp = Math.floor(Math.random()*arr35.length)" method to take a random number from 0-arr35.length as the index, and use arr35[temp] to get the value of the index position in the array, also It is a random number, and then dynamically added to the span before sorting by getting the id. After fetching, use "arr35.splice(temp,1);" to delete the random number in the array and reduce the length of the array by one. Loop 5 times to get 5 random numbers from 01-35 without repetition, and the same goes for the next two digits.

var arr35 = ["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17",                           
"18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35"];
var arr12 = ["01","02","03","04","05","06","07","08","09","10","11","12"];
var span = "";
var temp = "";
for(var i=1;i<6;i++){
    span = "span" + x + i;
    temp = Math.floor(Math.random()*arr35.length);//随机取一个数
    document.getElementById(span).innerText = arr35[temp];
    document.getElementById(span).style.backgroundColor = "red";
    arr35.splice(temp,1);//删除该位置的值}

Random number sorting: The insertion sort algorithm is used here, which only sorts the first five digits of each group of numbers. The amount of data is very small. After sorting, the id is dynamically added to the sorted span by obtaining the id.

//插入排序
function bubbleSort(array){    
var len = array.length;    
for (var i = 0; i < len; i++) {        
for (var j = i; j > 0 && array[j]<array[j-1]; j--) {            
var swap = array[j];  //第二个for循环使元素比较并移动到合适位置
            array[j] = array[j-1];
            array[j-1] = swap;
        }
    }    return array;
}

The remaining JS, HTML, and CSS technologies will not be introduced in detail.

Complete code:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>大乐透号码生成器</title>
<style type="text/css">
#table{
width:800px; 
height:500px;
margin:10px;
border:2px 
solid 
#000000;
box-shadow: 10px 10px 5px;border-radius:50px;}
.buttonStyle{height:40px;margin:20px;font-size:20px;background-color:#6495ED;color:white;border-radius:10px;}
.oneStyle{margin-left:200px;margin-top:10px;font-family:sans-serif;font-size:20px;}
span{border-radius: 50%;color: #FFFFFF;padding:3px;font-size:13px;}
</style>
</head>
<body>
    <p id="table">
        <p>
            <h1 id="大乐透号码生成器">大乐透号码生成器</h1>
        </p>
        <p class="oneStyle">
                        生成组数:<select id="zushu" style="width:150px;height:30px;margin:10px;" onchange="select()">
                        <option value="1">生成 1 组号码</option>
                        <option value="2">生成 2 组号码</option>
                        <option value="3">生成 3 组号码</option>
                        <option value="4">生成 4 组号码</option>
                        <option value="5">生成 5 组号码</option>
                    </select>
        </p>
        <p class="oneStyle">
        随机号码1:<span id="span11"></span> <span id="span12"></span> <span id="span13"></span> 
        <span id="span14"></span> <span id="span15"></span>
           <span id="span16"></span> <span id="span17"></span><br>
        随机号码2:<span id="span21"></span> <span id="span22"></span> <span id="span23"></span> 
        <span id="span24"></span> <span id="span25"></span>
           <span id="span26"></span> <span id="span27"></span><br>
        随机号码3:<span id="span31"></span> <span id="span32"></span> <span id="span33"></span> 
        <span id="span34"></span> <span id="span35"></span>
           <span id="span36"></span> <span id="span37"></span><br>
        随机号码4:<span id="span41"></span> <span id="span42"></span> <span id="span43"></span> 
        <span id="span44"></span> <span id="span45"></span>
           <span id="span46"></span> <span id="span47"></span><br>
        随机号码5:<span id="span51"></span> <span id="span52"></span> <span id="span53"></span> 
        <span id="span54"></span> <span id="span55"></span>
           <span id="span56"></span> <span id="span57"></span><br>
        </p>
        <p class="oneStyle">
        排序后码1:<span id="span61"></span> <span id="span62"></span> <span id="span63"></span> 
        <span id="span64"></span> <span id="span65"></span>
           <span id="span66"></span> <span id="span67"></span><br>
        排序后码2:<span id="span71"></span> <span id="span72"></span> <span id="span73"></span> 
        <span id="span74"></span> <span id="span75"></span>
           <span id="span76"></span> <span id="span77"></span><br>
        排序后码3:<span id="span81"></span> <span id="span82"></span> <span id="span83"></span> 
        <span id="span84"></span> <span id="span85"></span>
           <span id="span86"></span> <span id="span87"></span><br>
        排序后码4:<span id="span91"></span> <span id="span92"></span> <span id="span93"></span> 
        <span id="span94"></span> <span id="span95"></span>
           <span id="span96"></span> <span id="span97"></span><br>
        排序后码5:<span id="span101"></span> <span id="span102"></span> <span id="span103"></span> 
        <span id="span104"></span> <span id="span105"></span>
           <span id="span106"></span> <span id="span107"></span><br>
        </p>
        <p style="text-align:center">
            <input class="buttonStyle" id="fiveNumber" type="button" onclick="number()">
        </p>
    </p>
    <script type="text/javascript">
        var table = document.getElementById("table");        
        var width = document.documentElement.clientWidth;  //浏览器可见区域宽
        var height = document.documentElement.clientHeight;  //浏览器可见区域高        
        table.style.marginLeft = ((width-800)/2)+"px";        
        table.style.marginTop = ((height-700)/2)+"px";
        var val = "1";
        document.getElementById("fiveNumber").setAttribute("title","生成 1 组号码");
        document.getElementById("fiveNumber").setAttribute("value","生成 1 组号码");        
        //随机生成五组号码
        function select()
        {
            val = document.getElementById("zushu").value;            
            var des = "生成 " + val + " 组号码";
            document.getElementById("fiveNumber").setAttribute("title",des);
            document.getElementById("fiveNumber").setAttribute("value",des);
        }        //随机生成五组号码
        function number()
        {            for(var n=1;n<11;n++){                
        for(var m=1;m<8;m++){                    
        var a = "span" + n + m;
                    document.getElementById(a).innerText = "";//每次点击按钮先清空上一次数据                    
                    document.getElementById(a).style.backgroundColor = "white";
                }
            }            for(var x=1;x<(parseInt(val)+1);x++){                //从35个号码里面无放回地取5位
                var arr35 = ["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17",               
                            "18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35"];       
                                     //从12个号码里面无放回地取2位
                var arr12 = ["01","02","03","04","05","06","07","08","09","10","11","12"];                
                var span = "";                
                var temp = "";                
                for(var i=1;i<6;i++){
                    span = "span" + x + i;
                    temp = Math.floor(Math.random()*arr35.length);//随机取一个数                    
                    document.getElementById(span).innerText = arr35[temp];
                    document.getElementById(span).style.backgroundColor = "red";
                    arr35.splice(temp,1);//删除该位置的值                
                    }                
                    for(var j=6;j<8;j++){
                    span = "span" + x + j;
                    temp = Math.floor(Math.random()*arr12.length);//随机取一个数                    
                    document.getElementById(span).innerText = arr12[temp];
                    document.getElementById(span).style.backgroundColor = "blue";
                    arr12.splice(temp,1);//删除该位置的值                
                    }
            }            
            for(var x=1;x<(parseInt(val)+1);x++){                
            var span = "";                //前五位排序
                var arr = new Array(5);                
                for(var y=0;y<5;y++){
                    span = "span" + x + (y+1);
                    arr[y] = document.getElementById(span).lastChild.nodeValue;
                }                var a = bubbleSort(arr);                
                for(var l=0;l<5;l++){
                    span = "span" + (x+5) + (l+1);
                    document.getElementById(span).innerText = a[l];
                    document.getElementById(span).style.backgroundColor = "red";
                }                //后两位排序
                var spann6 = "span" + x + "6";                
                var spann7 = "span" + x + "7";                
                var spanm6 = "span" + (x+5) + "6";                
                var spanm7 = "span" + (x+5) + "7";                
                var span6 = document.getElementById(spann6).lastChild.nodeValue;                
                var span7 = document.getElementById(spann7).lastChild.nodeValue;                
                if(parseInt(span6) > parseInt(span7)){
                    document.getElementById(spanm6).innerText = span7;
                    document.getElementById(spanm7).innerText = span6;
                }else{
                    document.getElementById(spanm6).innerText = span6;
                    document.getElementById(spanm7).innerText = span7;
                }
                document.getElementById(spanm6).style.backgroundColor = "blue";
                document.getElementById(spanm7).style.backgroundColor = "blue";
            }
        }        //插入排序
        function bubbleSort(array){            
        var len = array.length;            
        for (var i = 0; i < len; i++) {                
        for (var j = i; j > 0 && array[j]<array[j-1]; j--) {                       
        var swap = array[j];  //第二个for循环使元素比较并移动到合适位置                       
        array[j] = array[j-1];
                       
                   array[j-1] = swap;
                }
            }            return array;
        }    </script></body></html>


Browser opening effect:


# #

The above is the detailed content of Example analysis of using JavaScript to generate lottery numbers. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.