|
1 | 1 |
|
2 | | -# Pseudo-random generator |
| 2 | +# Псевдовипадковий генератор |
3 | 3 |
|
4 | | -There are many areas where we need random data. |
| 4 | +Є багато областей, де нам потрібні випадкові дані. |
5 | 5 |
|
6 | | -One of them is testing. We may need random data: text, numbers, etc. to test things out well. |
| 6 | +Одна з них – тестування. Нам можуть знадобитися випадкові дані: текст, числа тощо, щоб добре все перевірити. |
7 | 7 |
|
8 | | -In JavaScript, we could use `Math.random()`. But if something goes wrong, we'd like to be able to repeat the test, using exactly the same data. |
| 8 | +У JavaScript ми можемо використовувати `Math.random()`. Але якщо щось піде не так, ми хотіли б мати можливість повторити тест, використовуючи точно ті самі дані. |
9 | 9 |
|
10 | | -For that, so called "seeded pseudo-random generators" are used. They take a "seed", the first value, and then generate the next ones using a formula so that the same seed yields the same sequence, and hence the whole flow is easily reproducible. We only need to remember the seed to repeat it. |
| 10 | +Для цього використовуються так звані "сіяні псевдовипадкові генератори". Вони беруть "зерно", перше значення, а потім генерують наступні за допомогою формули, так що те саме насіння дає ту саму послідовність, а отже, весь потік легко відтворюється. Нам потрібно лише згадати зерно, щоб повторити його. |
11 | 11 |
|
12 | | -An example of such formula, that generates somewhat uniformly distributed values: |
| 12 | +Приклад такої формули, яка генерує рівномірно розподілені значення: |
13 | 13 |
|
14 | 14 | ``` |
15 | 15 | next = previous * 16807 % 2147483647 |
16 | 16 | ``` |
17 | 17 |
|
18 | | -If we use `1` as the seed, the values will be: |
| 18 | +Якщо ми використаємо `1` як зерно, то значення будуть такими: |
19 | 19 | 1. `16807` |
20 | 20 | 2. `282475249` |
21 | 21 | 3. `1622650073` |
22 | 22 | 4. ...and so on... |
23 | 23 |
|
24 | | -The task is to create a generator function `pseudoRandom(seed)` that takes `seed` and creates the generator with this formula. |
| 24 | +Завдання полягає в тому, щоб створити функцію-генератор `pseudoRandom(seed)`, яка приймає `seed` і створює генератор з цією формулою. |
25 | 25 |
|
26 | | -Usage example: |
| 26 | +Приклад використання: |
27 | 27 |
|
28 | 28 | ```js |
29 | 29 | let generator = pseudoRandom(1); |
|
0 commit comments