You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The simplest, but wrong solution would be to generate a value from `min` to `max` and round it:
3
+
가장 간단하지만 틀린 해답은 `min`에서 `max`까지의 값을 생성하고 반올림하는 것입니다.
4
4
5
5
```js run
6
6
functionrandomInteger(min, max) {
@@ -11,28 +11,28 @@ function randomInteger(min, max) {
11
11
alert( randomInteger(1, 3) );
12
12
```
13
13
14
-
The function works, but it is incorrect. The probability to get edge values `min` and `max` is two times less than any other.
14
+
`randomInteger` 함수는 동작하지만, 올바르지 않습니다. 에지값 `min`과 `max`가 나올 확률이 두 배 적습니다.
15
15
16
-
If you run the example above many times, you would easily see that `2` appears the most often.
16
+
위의 예시를 여러 번 실행하면, `2`가 가장 많이 나타나는 것을 알 수 있습니다.
17
17
18
-
That happens because `Math.round()` gets random numbers from the interval `1..3`and rounds them as follows:
18
+
`Math.round()`는 `1..3`구간에서 난수를 가져와 다음과 같이 반올림하기 때문입니다.
19
19
20
20
```js no-beautify
21
-
values from 1...to 1.4999999999 become 1
22
-
values from 1.5...to 2.4999999999 become 2
23
-
values from 2.5...to 2.9999999999 become 3
21
+
1 에서...1.4999999999까지의 값은 1 이 됩니다.
22
+
1.5에서 ...2.4999999999까지의 값은 2 가 됩니다.
23
+
2.5에서 ...2.9999999999까지의 값은 3 이 됩니다.
24
24
```
25
25
26
-
Now we can clearly see that `1` gets twice less values than `2`. And the same with `3`.
26
+
이제 `1`이 `2`보다 두 배 적게 나오는 것을 알 수 있습니다. `3`도 마찬가지입니다.
27
27
28
-
# The correct solution
28
+
# 올바른 해답
29
29
30
-
There are many correct solutions to the task. One of them is to adjust interval borders. To ensure the same intervals, we can generate values from `0.5 to 3.5`, thus adding the required probabilities to the edges:
30
+
올바른 해답이 많이 있지만 그중 하나는 구간을 조정하는 것입니다. 동일한 간격을 보장하기 위해서, `0.5 에서 3.5` 사이의 값을 생성하여, 에지값에 필요한 확률을 추가할 수 있습니다.
31
31
32
32
```js run
33
33
*!*
34
34
functionrandomInteger(min, max) {
35
-
//now rand is from (min-0.5) to (max+0.5)
35
+
//rand는 (min-0.5)에서 (max+0.5)까지 입니다.
36
36
let rand = min -0.5+Math.random() * (max - min +1);
37
37
returnMath.round(rand);
38
38
}
@@ -41,12 +41,12 @@ function randomInteger(min, max) {
41
41
alert( randomInteger(1, 3) );
42
42
```
43
43
44
-
An alternative way could be to use `Math.floor` for a random number from `min` to `max+1`:
44
+
다른 방법은 `min`에서 `max+1`까지의 난수에 `Math.floor`를 사용하는 것입니다.
45
45
46
46
```js run
47
47
*!*
48
48
functionrandomInteger(min, max) {
49
-
//here rand is from min to (max+1)
49
+
//rand는 min에서 (max+1)까지 입니다.
50
50
let rand = min +Math.random() * (max +1- min);
51
51
returnMath.floor(rand);
52
52
}
@@ -55,12 +55,12 @@ function randomInteger(min, max) {
55
55
alert( randomInteger(1, 3) );
56
56
```
57
57
58
-
Now all intervals are mapped this way:
58
+
모든 구간이 다음과 같이 매핑되었습니다.
59
59
60
60
```js no-beautify
61
-
values from 1...to1.9999999999become 1
62
-
values from 2...to2.9999999999become 2
63
-
values from 3...to3.9999999999become 3
61
+
1...에서1.9999999999까지의 값은 1 이 됩니다.
62
+
2...에서2.9999999999까지의 값은 2 가 됩니다.
63
+
3...에서3.9999999999까지의 값은 3 이 됩니다.
64
64
```
65
65
66
-
All intervals have the same length, making the final distribution uniform.
Copy file name to clipboardExpand all lines: 1-js/05-data-types/02-number/9-random-int-min-max/task.md
+5-5
Original file line number
Diff line number
Diff line change
@@ -2,19 +2,19 @@ importance: 2
2
2
3
3
---
4
4
5
-
# A random integer from min to max
5
+
# 최솟값에서 최대값까지의 무작위 정수
6
6
7
-
Create a function `randomInteger(min, max)` that generates a random *integer* number from `min` to `max` including both `min` and `max` as possible values.
7
+
가능한 값으로 `min`과 `max`을 포함하여 `min`에서 `max`까지 무작위 *정수*를 생성하는 함수 `randomInteger(min, max)`을 작성해 보세요.
8
8
9
-
Any number from the interval `min..max`must appear with the same probability.
9
+
`min..max`구간의 어떤 숫자라도 같은 확률로 나타나야 합니다.
10
10
11
11
12
-
Examples of its work:
12
+
예시:
13
13
14
14
```js
15
15
alert( randomInteger(1, 5) ); // 1
16
16
alert( randomInteger(1, 5) ); // 3
17
17
alert( randomInteger(1, 5) ); // 5
18
18
```
19
19
20
-
You can use the solution of the [previous task](info:task/random-min-max) as the base.
20
+
[이전 과제](info:task/random-min-max)의 해답을 바탕으로 사용할 수 있습니다.
0 commit comments