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 precision loss can cause both increase and decrease of a number. In this particular case the number becomes a tiny bit less, that's why it rounded down.
10
-
11
-
And what's for `1.35`?
7
+
قد يتسبب ضياع الدقة في زيادة أو نقصان أي عدد. يكون العدد في هذه الحالة أقل بقليل من قيمته الفعلية، ولهذا يُدَوَّر للأسفل. ماذا عن العدد `1.35`؟
Note that `63.5` has no precision loss at all. That's because the decimal part `0.5`is actually `1/2`. Fractions divided by powers of`2`are exactly represented in the binary system, now we can round it:
24
+
لاحظ عدم وجود أي ضياع في دقة العدد `63.5`. ذلك لأن الجزء العشري `0.5`يساوي `1/2`. يمكن تمثيل الأجزاء المقسومة على`2`تُمَثَّل بشكل صحيح في النظام الثنائي. يمكننا تقريب العدد الآن:
According to the documentation `Math.round` and `toFixed` both round to the nearest number: `0..4` lead down while `5..9` lead up.
8
7
9
-
For instance:
8
+
تُدَوِّر كلًا من `Math.round` و `toFixed` العدد إلى أقرب عدد له وفقًا للتوثيق: الأجزاء من `0..4` تُدَوَّر للأسفل، بينما الأجزاء `5..9` تثدَوَّر للأعلى.
9
+
10
+
مثلًا:
11
+
10
12
11
13
```js run
12
14
alert( 1.35.toFixed(1) ); // 1.4
13
15
```
14
16
15
-
In the similar example below, why is`6.35`rounded to `6.3`, not`6.4`?
17
+
في المثال المشابه أدناه، لِمَ تُدَوَّر`6.35`إلى `6.3`، وليس`6.4`؟
ذلك لأن `i` لن يساوي `10` أبدًا. نفذ الشيفرة التالية لرؤية قيم `i`:
4
2
5
3
```js run
6
4
let i =0;
@@ -10,8 +8,4 @@ while (i < 11) {
10
8
}
11
9
```
12
10
13
-
None of them is exactly `10`.
14
-
15
-
Such things happen because of the precision losses when adding fractions like `0.2`.
16
-
17
-
Conclusion: evade equality checks when working with decimal fractions.
11
+
لا توجد قيمة تساوي `10` تمامًا. تحدث مثل هذه الأمور بسبب ضياع الدقة عند إضافة الأجزاء مثل `0.2`. الخلاصة، تجنب التحقق من المساواة عند التعامل مع الأجزاء العشرية.
Copy file name to clipboardExpand all lines: 1-js/05-data-types/02-number/8-random-min-max/task.md
+4-5Lines changed: 4 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,13 @@
1
-
importance: 2
1
+
الأهمية: 2
2
2
3
3
---
4
4
5
-
# A random number from min to max
5
+
# رقم عشوائي من العدد الأدنى إلى الأقصى
6
6
7
-
The built-in function `Math.random()` creates a random value from `0`to`1` (not including `1`).
7
+
تُنشِئ الدالة `Math.random()` المُضَمَنَة في اللغة قيمة عشوائية بين `0`و`1` (ليس بما في ذلك `1`). اكتب الدالة `random(min, max)` لتوليد عدد عشري عشوائي من `min` إلى `max` (بما لا يتضمن `max`).
8
8
9
-
Write the function `random(min, max)` to generate a random floating-point number from `min` to `max` (not including `max`).
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,26 @@ 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
+
الدالة تعمل لكنها خطأ. احتمال ظهور القيم الطرفية `min`و`max`أقل بمرتين من باقي القيم. إن شغلنا المثال أعلاه لعدة مرات، فينرى ظهور `2` بصورة أكبر.
15
15
16
-
If you run the example above many times, you would easily see that `2` appears the most often.
17
-
18
-
That happens because `Math.round()` gets random numbers from the interval `1..3` and rounds them as follows:
16
+
يحدث ذلك لأن `Math.round()` تأخذ رقما من الفترة `1..3` وتُدَوِرها كما يلي:
19
17
20
18
```js no-beautify
21
19
values from 1... to 1.4999999999 become 1
22
20
values from 1.5... to 2.4999999999 become 2
23
21
values from 2.5... to 2.9999999999 become 3
24
22
```
25
23
26
-
Now we can clearly see that `1`gets twice less values than `2`. And the same with`3`.
24
+
نلاحظ الآن أن لدى `1`قيم أقل بمرتين من `2` وكذلك`3`.
27
25
28
-
# The correct solution
26
+
# الطريقة الصحيحة
29
27
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:
28
+
يوجد العديد من الطرق الصحيحة لحل هذه المهمة. إحداها هو بتعديل حدود الفترة. للتأكد من وجود فرص متساوية، نُوَلِّد قيمًا من `0.5` إلى `3.5`، ثم إضافة الاحتمالات الممكنة للأطراف:
31
29
32
30
```js run
33
31
*!*
34
32
functionrandomInteger(min, max) {
35
-
//now rand is from (min-0.5) to (max+0.5)
33
+
//(max+0.5) إلى (min-0.5) التقريب الآن من
36
34
let rand = min -0.5+Math.random() * (max - min +1);
37
35
returnMath.round(rand);
38
36
}
@@ -41,12 +39,12 @@ function randomInteger(min, max) {
41
39
alert( randomInteger(1, 3) );
42
40
```
43
41
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`:
45
43
46
44
```js run
47
45
*!*
48
46
functionrandomInteger(min, max) {
49
-
//here rand is from min to (max+1)
47
+
//(max+1) إلى min التقريب الآن من
50
48
let rand = min +Math.random() * (max +1- min);
51
49
returnMath.floor(rand);
52
50
}
@@ -55,12 +53,12 @@ function randomInteger(min, max) {
55
53
alert( randomInteger(1, 3) );
56
54
```
57
55
58
-
Now all intervals are mapped this way:
56
+
جميع الفترات أصبحت متوازنة الآن:
59
57
60
58
```js no-beautify
61
59
values from 1... to 1.9999999999 become 1
62
60
values from 2... to 2.9999999999 become 2
63
61
values from 3... to 3.9999999999 become 3
64
62
```
65
63
66
-
All intervals have the same length, making the final distribution uniform.
64
+
لدى جميع الفترات الطول ذاته مما يجعل التوزيع النهائي موحدًا.
0 commit comments