java-school logo

Math

var random = Math.random() * 10;
random = random.toPrecision(3);
alert("랜덤값 : " + random);

var ceil = Math.ceil(random);
alert("올림값 : " + ceil);

var floor = Math.floor(random);
alert("내림값 : " + floor);

var round = Math.round(random);
alert("반올림 : " + round);

로또번호 생성기 자바스크립트 버전

var lotto = new Array();
var index = 0;
while(true) {
	var check = true;
	var ball = Math.random() * 45;
	ball = Math.floor(ball) + 1;
	for (var i = 0; i < lotto.length;i++) {
		if (lotto[i] == ball) {
			check = false;
			break;
		}
	}
	if (check) {
		lotto[index++] = ball
	}
	if (index > 5) {
		break;
	}
}

alert("예상로또번호: " + lotto.toString());
참고