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
66function randomInteger (min , max ) {
@@ -11,56 +11,52 @@ function randomInteger(min, max) {
1111alert ( 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- * ! *
3433function 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
4139alert ( 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- * ! *
4845function 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
5551alert ( 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+ ทุกช่วงมีความยาวเท่ากัน ทำให้การกระจายสุดท้ายสม่ำเสมอ
0 commit comments