1. 랜덤 함수
{
int num1 = (int) (Math.random()*10);
int num2 = (int) (Math.random()*10);
int num3 = (int) (Math.random()*10);
System.out.println(num1 +"," + num2 +","+ num3 );
System.out.println("=========================");
Random random = new Random();
int num4 = random.nextInt(10);
int num5 = random.nextInt(10);
int num6 = random.nextInt(10);
System.out.println(num4 + ","+ num5 + "," + num6);
}
}
실행결과
1.
9,9,9
=========================
8,2,2
2.
2,8,6
=========================
0,3,2
2. 중복 되지 않는 랜덤 함수
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random randomObj = new Random();
System.out.println("1.");
int [] arr = new int[5];
for ( int i = 0; i < arr.length ; i++){
arr[i] = randomObj.nextInt(arr.length);
for(int j = i-1; j >=0; j--){
if(arr[i]==arr[j]){
i--;
break;
}
}
}
for( int i = 0; i<arr.length ; i++)
System.out.print(arr[i]);
}
댓글 없음:
댓글 쓰기