로또번호 생성(자바스크립트)

로또번호 생성(자바스크립트)

로또 번호를 생성해 보겠습니다. 클릭하면 번호가 나옵니다. 소스도 함께 첨부합니다.

 

 

고정수를 넣을 수 있습니다. 공백없이 콤마(,)로 구분하여 수를 넣어주세요.(1,2,3,...)

 

제외수를 넣을 수 있습니다. 공백없이 콤마(,)로 구분하여 수를 넣어주세요.(1,2,3,...)

 

로또 관련 글

🏆로또 현재까지 가장 많이 나온 당첨 숫자 알아보기
🏆로또 회차 당 적정 당첨자 수는 얼마나 될까?
🏆거액을 타는 로또 당첨 비법이 있다고?
🏆로또 회차별 당첨결과 받기

 

<script>
 
function lotto() {
 
    var ctx = document.getElementById("canvas").getContext("2d");
    ctx.clearRect(0,0, canvas.width, canvas.height);
 
    lottoNum = new Array(45);
    for (i = 0; i < 45; i++) {
        lottoNum[i] = i + 1;
    }
 
    var rNum;
    myNum = new Array(6);
    for (i = 0; i < 6; i++) {
        rNum = parseInt(Math.random()*lottoNum.length);
        myNum[i] = lottoNum[rNum];
        lottoNum.splice(rNum,1); //해당 배열 한칸 줄임
    }
 
 
    for (i = 0; i < 6; i++) {
        for (j = 0; j <= i; j++) {
            if(myNum[i] <= myNum[j]) {
                k = myNum[i];
                myNum[i] = myNum[j];
                myNum[j] = k;
            }
        }
    } 
    document.getElementById('number').innerHTML = myNum;
    
    ctx.font='22px Arial';
    ctx.textAlign = "center";
//    ctx.textBaseLine = "middle";
    for(i=0;i<6;i++) {
        switch(Math.ceil(myNum[i]/10)){
            case 1:
                ctx.beginPath();
                ctx.arc(i*60+30,25,20,0,2*Math.PI,false);
                ctx.fillStyle = "rgb(250,196,0)";
                ctx.fill();
                ctx.closePath();
                break;
            case 2:
                ctx.beginPath();
                ctx.arc(i*60+30,25,20,0,2*Math.PI,false);
                ctx.fillStyle = "rgb(104,200,242)";
                ctx.fill();
                ctx.closePath();
                break;
            case 3:
                ctx.beginPath();
                ctx.arc(i*60+30,25,20,0,2*Math.PI,false);
                ctx.fillStyle = "rgb(254,114,114)";
                ctx.fill();
                ctx.closePath();
                break;
            case 4:
                ctx.beginPath();
                ctx.arc(i*60+30,25,20,0,2*Math.PI,false);
                ctx.fillStyle = "rgb(170,170,170)";
                ctx.fill();
                ctx.closePath();
                break;
            case 5:
                ctx.beginPath();
                ctx.arc(i*60+30,25,20,0,2*Math.PI,false);
                ctx.fillStyle = "rgb(176,216,65)";
                ctx.fill();
                ctx.closePath();
                break;
        }
        ctx.fillStyle = "white";
        ctx.fillText(myNum[i],i*60+30,32);
    }
 
}
 
</script>
 
<input id="btn" type="button" onclick="lotto()" value="Click">
<div id="number" style="font-size:20px;border:1px solid;width:150px;height:20px;text-align:center;padding:10px;"></div>
 
 
<body>
    <canvas id="canvas" height="50" width="360"></canvas>
</body>
cs

간단히 설명을 드리면 배열에 로또번호 1에서 45까지 담습니다. 내 번호배열에 무작위로(Math.random()) 담고, 담은 번호는 로또번호 배열에서 빼버립니다(splice로 배열을 줄임). 여섯 숫자를 담으면서 반복하고 이렇게 선택된 번호를 오름차순으로 재배열합니다. 그 후 캔버스에 그림을 그립니다. 번호에 나누기 10을 하고 올림(Math.ceil)하여 1~5까지 Case로 구분하고 채워진 원을 그리고 그 안에 번호를 썼습니다. 부디 행운이 있길 빌겠습니다.

댓글

Designed by JB FACTORY