자바스크립트로 로또번호 생성하는 소스를 만들어봤다.
-구성-
단순 HTML 파일 (웹브라우저로 로컬실행시 동작)
-기능 구현(구현한 순서대로 추가중)-
1. 1부터 45까지의 숫자 중 중복되지않는 무작위 숫자 6개 추출.
2. 지금까지의 로또 당첨번호와 중복되지 않게 체크.(매주 업데이트 필요)
3. 번호 생성시 클립보드 저장.
4. 15주간 미출현한 번호 포함여부 체크.(매주 업데이트 필요)
-향후 구현예정-
- 기능구현2) 와 같은 로직 추가에 따라 구성 확장(DB 추가 등)
- 기능구현2)의 자동화(현재는 회차별 변수에 당첨번호를 입력한 상태)
-. 동행복권쪽에 api로 제공하지 않으면 웹크롤링 필요
추출번호 |
---|
스크립트 부분
<script type="text/javascript">
var lotto_old = [];
var lotto_unseen = [];
$(document).ready(function(){
//15주간 미출현 번호 변수에 저장
lotto_unseen = [15,20,25,30,45];
//모든 회차 당첨번호 변수에 저장
lotto_old[0]="3,11,34,42,43,44";
//이하생략
});
//로또번호 추출 버튼 클릭시 호출함수
function lottoGen() {
//최종 추출할 로또 번호
var lotto = [];
//6자리 번호 생성 후 기존 회차의 당첨번호들과 비교를 위한 반복문
while (true) {
//초기화
lotto = [];
//중복없는 추출된 번호들 숫자 카운트
var count = 0;
//중복된 수 인지 판단하는 변수
var overl = true;
//6자리 번호 생성을 위한 반복문
while (count < 6) {
var number = 0;
// 1~45사이에 랜덤번호 추출
number = parseInt(Math.random() * 45) + 1;
//중복체크
for (var i = 0; i < count; i++) {
if (lotto[i] == number) {
overl = false;
}
}
//중복이 아닐시 lotto 에 추가
if (overl) {
lotto.push(number);
count++;
}
//초기화
overl = true;
}
//추출한 6자리 숫자를 정렬
lotto.sort(function(a, b) {
// 오름차순
return a - b;
});
//기존 회차의 당첨번호와 중복체크
for (var i = 0; i < lotto_old.length-1; i++) {
if (lotto_old[i] == lotto) {
console.log("lotto_old["+i+"] : "+lotto_old[i]);
continue;
}
}
console.log("lotto : "+lotto);
//15주간 미출현 번호 포함여부 체크
var isUnseen = 0;
for (var i = 0; i <= lotto_unseen.length-1; i++) {
if (lotto.indexOf(lotto_unseen[i]) != "-1") {
console.log("lotto_unseen["+i+"] : "+lotto_unseen[i]);
isUnseen++;
}else{
console.log("lotto.indexOf(lotto_unseen["+i+"]) : "+lotto.indexOf(lotto_unseen[i]));
}
}
if(isUnseen == 0){
continue;
}
break;
}
//로또 번호 테이블에 추가
var html = "";
html += "";
html += lotto+"
";
html += '';
html += '';
$("#lotto_table").find("tbody").append(html);
//클립보드 복사
copy(lotto);
$("#clipAlert").text("클립보드에 저장되었습니다.\n"+lotto);
$("#clipAlert").css("visibility","visible");
setTimeout(function() {
$("#clipAlert").css("visibility","hidden");
}, 500);
//온클릭함수 세팅
//clickTrEvent();
}
//클립보드 저장 함수
function copy(val) {
var dummy = document.createElement("textarea");
document.body.appendChild(dummy);
dummy.value = val;
dummy.select();
document.execCommand("copy");
document.body.removeChild(dummy);
}
</script>
HTML 부분
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<style>
.btn_register {
width: 105px;
height: 36px;
color: #fff;
cursor: pointer;
outline: none;
}
</style>
<div style="width:100%;text-align: center;">
<span id="clipAlert" style="visibility:hidden; height:50px; width:300px;text-align: center;">
</span>
<br/>
<br/>
<button type="button" class="btn_register thema_apply" onclick="lottoGen()" >로또번호생성</button>
</div>
<br/>
<div id="lotto_div" style="height: 300px;overflow: auto;">
<table id="lotto_table" style="width:100%">
<colgroup class="bg1">
<col style="width:100%"/>
</colgroup>
<thead>
<tr style="width: 100%;">
<th>추출번호</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>