//중복값 없이 랜덤 출력하기
//이중루프를 이용하여 하는 방법도 있음(필터링로직)
public class Exam_04 {
public static void main(String[] args) {
/*
* System.out.println((int)(Math.random()*5+1)); // 1~5
* System.out.println((int)(Math.random()*5+1)); // 1~5
* System.out.println((int)(Math.random()*5+1)); // 1~5
* System.out.println((int)(Math.random()*5+1)); // 1~5
* System.out.println((int)(Math.random()*5+1)); // 1~5
*/
// ctrl+shift+/ : 블록지정
int[] numbers=new int[] {1,2,3,4,5};
//카드섞기기법
//숫자를 저장해놓은 배열의 인덱스를 뒤죽박죽 난수로 섞으면 = 중복값없는 난수 뽑는 것과 같은 효과
for(int i=0;i<100;i++) { //카드를 100번 섞는 효과
int x = (int)((Math.random())*5);
int y = (int)((Math.random())*5);
int temp=numbers[x];
numbers[x]=numbers[y];
numbers[y]=temp;
}
System.out.println(numbers[0] + " : " + numbers[1] + " : " + numbers[2]);
}
}
Comment