프로그래밍/ㆍC/C++/Java

[Java] LottoNumber - 랜덤함수, HashSet으로 로또 번호 추출하기 예제

쪼재 2012. 2. 12. 13:47
Math.random() 와 HastSet 으로 구현된 클래스 임의의 숫자 1 ~ 45 를 추출하여 HashSet에 저장한다. HashSet은 중복을 허용하지 않기 때문에 알아서 중복된 숫자가 추출될 경우를 걸러내어 준다.
// LottoNumber.class
 

public class LottoNumber {

	public static void main(String[] args) {
		
		HashSet set = new HashSet();
		
		
		while(set.size()<=6) {
			set.add((int)(Math.random() * 44) + 1);
		}
		
		for(int a : set) {
			System.out.println(a);
		}
		
	}
}