Skip to content

Commit f227ac0

Browse files
committed
Refactor number formatting and rounding in article.md
1 parent 063818f commit f227ac0

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed
Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# The simple but wrong solution
1+
# วิธีแก้ที่ง่ายแต่ไม่ถูกต้อง
22

3-
The simplest, but wrong solution would be to generate a value from `min` to `max` and round it:
3+
วิธีที่ง่ายที่สุดแต่ไม่ถูกต้องคือการสร้างค่าจาก `min` ถึง `max` แล้วปัดเศษ:
44

55
```js run
66
function randomInteger(min, max) {
@@ -11,56 +11,52 @@ function randomInteger(min, max) {
1111
alert( randomInteger(1, 3) );
1212
```
1313

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+
ฟังก์ชันนี้ทำงานได้ แต่ไม่ถูกต้อง โอกาสที่จะได้ค่าขอบ `min` และ `max` น้อยกว่าค่าอื่นๆ สองเท่า
1515

16-
If you run the example above many times, you would easily see that `2` appears the most often.
16+
ถ้าคุณรันตัวอย่างข้างบนหลายๆ ครั้ง คุณจะเห็นว่า `2` ปรากฏบ่อยที่สุด
1717

18-
That happens because `Math.round()` gets random numbers from the interval `1..3` and rounds them as follows:
18+
นี่เกิดขึ้นเพราะ `Math.round()` รับตัวเลขสุ่มจากช่วง `1..3` และปัดเศษดังนี้:
1919

2020
```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
2424
```
2525

26-
Now we can clearly see that `1` gets twice less values than `2`. And the same with `3`.
26+
เราเห็นชัดว่า `1` ได้ค่าน้อยกว่า `2` สองเท่า และเช่นเดียวกันกับ `3`
2727

28-
# The correct solution
28+
# วิธีแก้ที่ถูกต้อง
2929

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` เพื่อเพิ่มโอกาสที่ต้องการให้กับขอบ:
3131

3232
```js run
33-
*!*
3433
function randomInteger(min, max) {
35-
// now rand is from (min-0.5) to (max+0.5)
34+
// ตอนนี้ rand อยู่ระหว่าง (min-0.5) ถึง (max+0.5)
3635
let rand = min - 0.5 + Math.random() * (max - min + 1);
3736
return Math.round(rand);
3837
}
39-
*/!*
4038

4139
alert( randomInteger(1, 3) );
4240
```
4341

44-
An alternative way could be to use `Math.floor` for a random number from `min` to `max+1`:
42+
อีกวิธีหนึ่งคือใช้ `Math.floor` สำหรับตัวเลขสุ่มจาก `min` ถึง `max+1`:
4543

4644
```js run
47-
*!*
4845
function randomInteger(min, max) {
49-
// here rand is from min to (max+1)
46+
// ตรงนี้ rand อยู่ระหว่าง min ถึง (max+1)
5047
let rand = min + Math.random() * (max + 1 - min);
5148
return Math.floor(rand);
5249
}
53-
*/!*
5450

5551
alert( randomInteger(1, 3) );
5652
```
5753

58-
Now all intervals are mapped this way:
54+
ตอนนี้ทุกช่วงถูกแมปดังนี้:
5955

6056
```js no-beautify
61-
values from 1 ... to 1.9999999999 become 1
62-
values from 2 ... to 2.9999999999 become 2
63-
values from 3 ... to 3.9999999999 become 3
57+
ค่าจาก 1 ... ถึง 1.9999999999 กลายเป็น 1
58+
ค่าจาก 2 ... ถึง 2.9999999999 กลายเป็น 2
59+
ค่าจาก 3 ... ถึง 3.9999999999 กลายเป็น 3
6460
```
6561

66-
All intervals have the same length, making the final distribution uniform.
62+
ทุกช่วงมีความยาวเท่ากัน ทำให้การกระจายสุดท้ายสม่ำเสมอ

1-js/05-data-types/02-number/9-random-int-min-max/task.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@ importance: 2
22

33
---
44

5-
# A random integer from min to max
5+
# สุ่มจำนวนเต็มจาก min ถึง max
66

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+
สร้างฟังก์ชัน `randomInteger(min, max)` ที่สร้างตัวเลขจำนวนเต็มสุ่มจาก `min` ถึง `max` โดยรวมทั้ง `min` และ `max` เป็นค่าที่เป็นไปได้ด้วย
88

9-
Any number from the interval `min..max` must appear with the same probability.
9+
ตัวเลขใดๆ จากช่วง `min..max` ต้องปรากฏด้วยความน่าจะเป็นเท่ากัน
1010

11-
12-
Examples of its work:
11+
ตัวอย่างการทำงาน:
1312

1413
```js
1514
alert( randomInteger(1, 5) ); // 1
1615
alert( randomInteger(1, 5) ); // 3
1716
alert( randomInteger(1, 5) ); // 5
1817
```
1918

20-
You can use the solution of the [previous task](info:task/random-min-max) as the base.
19+
คุณสามารถใช้วิธีแก้ของ [โจทย์ก่อนหน้า](info:task/random-min-max) เป็นพื้นฐานได้

0 commit comments

Comments
 (0)