1. Math.random()
Math.random() 메소드는 0이상 1미만 사이의 실수 범위에서 난수를 발생시킨다.
(1) 예문1
- 0이상 1미만의 실수 난수 3개 생성 시키기
1
2
3
4
5
6
7
8
9
10
|
public class Test
{
public static void main(String[] args)
{
for(int i=0;i<3;i++)
{
System.out.println(Math.random() + " ");
}
}
}
|
cs |
(2) 예문2
- 1이상 3미만의 실수 난수 3개 생성 시키기
1
2
3
4
5
6
7
8
9
10
11
|
class RandomTest2
{
public static void main(String[] args)
{
for(int i=0;i<3;i++)
{
System.out.println(Math.random()*3);
}
}
}
|
cs |
(3) 예문3
- 1이상 3미만의 정수 난수 3개 생성 시키기
1
2
3
4
5
6
7
8
9
10
11
|
class RandomTest3
{
public static void main(String[] args)
{
for(int i=0;i<3;i++)
{
System.out.println((int)(Math.random()*3));
}
}
}
|
cs |
'Java > Day9' 카테고리의 다른 글
[Java] 아스키코드 & 유니코드 (0) | 2021.11.23 |
---|---|
[Java] 메소드 오버로딩 & 메소드 시그니처 (0) | 2021.11.12 |