0.0 ~ 1에 무한히 가까운 수를 제공합니다.
ex)
0.0
0.001
0.0000000001
....
0.9
0.999999999
0.99999999999999
100가지의 랜덤숫자를 출력해봅시다.
자바코드입력
for (int i=0; i<100; i++) {
System.out.println(Math.random());
}
콘솔 창 결과
0.06359764759898134
0.2683864463613098
0.9702648216570791
0.485595850959298
0.9788890525367653
0.4633746748255868
0.8289908475362834
0.2591187291964018
0.4278812375430615
0.1938362951445235
0.8503521705262688
0.5644105867896406
0.914259611259475
0.20302680593348743
0.29395511105260386
0.9972037487152107
0.9886288422441524
0.34230263640404146
0.6859581055114398
0.1767036322080574
0.278988244269178
0.47558298486852246
0.39750983378645166
0.970554454449064
0.2115615097796174
0.17814698974054044
0.126935664886471
0.20958987669271167
0.009169507381095343
0.08770720581270319
0.6975686393391298
0.5945481632941942
0.2740615550910993
0.8369884565482772
0.5684717845585563
0.4588440414762025
0.9630966832393603
0.5995817107252539
0.7219011137200522
0.5446553170141533
0.8453937123751857
0.414629937171178
0.13920930051865732
0.9739944138675843
0.5960446611138843
0.6209437343557945
0.9059924768518803
0.15210085742886137
0.5242135832044599
0.9274319534237735
0.09763317246165881
0.427403382458292
0.5599182995060765
0.8742730158566389
0.9879952921231104
0.9504654207638156
0.5310201689744228
0.698040493009099
0.7968769171656203
0.32374100813618467
0.9504229353616793
0.9040365922465197
0.1459299271464658
0.31463290832128177
0.5032083114215841
0.17037528118980683
0.6281058846947412
0.11625807817347222
0.1744130219608584
0.560896930767344
0.41228960343823506
0.38349044301762336
0.49037764354369506
0.4077842778464176
0.6026333883671762
0.5462644238273239
0.6600753283158065
0.6397895801101636
0.15120659171472028
0.7077868442655004
0.49755400132729233
0.7782711107527639
0.4829260309796738
0.7722193286985327
0.8826027641023882
0.30077685203737936
0.633335725797059
0.3145563981548919
0.1739797868548142
0.5807015784786803
0.6226542903842387
0.10483994984806488
0.4280151809996996
0.15158355691517345
0.9936113671374929
0.12951339231000203
0.495129359856331
0.6158951365378672
0.9259450123825956
0.41463387111171246
Math.random() * 숫자
: 입력한 숫자보다 작지만 무한히 가까운 수들이 만들어집니다.
예시) Math.random() * 12
: 12보다 작지만 무한히 가까운 수들이 만들어 집니다.
0.9 * 12 = 10.8
0.99 * 12 = 11.88
0.999 * 12 = 11.988
...
(int) Math.random() * (최댓값-최소값+1) + 최소값
(int) Math.random() * (최댓값-최소값+1) + 최소값
예시) 1 ~ 12까지의 랜덤 숫자 10가지 출력
(int) (Math.random() * (12-1+1)) + 1
→ (int) (Math.random() * 12) + 1
자바코드입력
for (int i=0; i<10; i++) {
System.out.println((int) (Math.random() * 12) + 1);
}
콘솔 창 결과
9
7
10
7
1
5
4
5
6
12
(int) (Math.random() * 13) + 12
자바코드입력
for (int i=0; i<10; i++) {
System.out.println((int) (Math.random() * 13) + 12);
}
콘솔 창 결과
18
20
20
13
12
16
15
13
22
24
좋은 글 잘보고 갑니다.
감사합니다