|
1 | 1 |
|
2 | | -# Pseudo-random generator |
| 2 | +# Generador pseudoaleatorio |
3 | 3 |
|
4 | | -There are many areas where we need random data. |
| 4 | +Hay muchas áreas en las que necesitamos datos aleatorios. |
5 | 5 |
|
6 | | -One of them is testing. We may need random data: text, numbers, etc. to test things out well. |
| 6 | +Uno de ellos es para testeo. Es posible que necesitemos datos aleatorios: texto, números, etc. para probar bien las cosas. |
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 | +En JavaScript, podríamos usar `Math.random()`. Pero si algo sale mal, nos gustaría poder repetir la prueba utilizando exactamente los mismos datos. |
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 | +Para eso, se utilizan los denominados "generadores pseudoaleatorios con semilla". Toman una "semilla" como primer valor, y luego generan los siguientes utilizando una fórmula; a partir de la misma semilla se produce la misma secuencia y así todo el flujo es fácilmente reproducible. Solo necesitamos recordar la semilla para repetirla. |
11 | 11 |
|
12 | | -An example of such formula, that generates somewhat uniformly distributed values: |
| 12 | +Un ejemplo de dicha fórmula, que genera valores distribuidos de manera algo uniforme: |
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 | +Si nosotros usamos `1` como semilla, los valores serán: |
19 | 19 | 1. `16807` |
20 | 20 | 2. `282475249` |
21 | 21 | 3. `1622650073` |
22 | | -4. ...and so on... |
| 22 | +4. ...y así... |
23 | 23 |
|
24 | | -The task is to create a generator function `pseudoRandom(seed)` that takes `seed` and creates the generator with this formula. |
| 24 | +La tarea es crear una función generadora `pseudoRandom (seed)` que toma `seed` y crea el generador con esta fórmula. |
25 | 25 |
|
26 | | -Usage example: |
| 26 | +Ejemplo de uso |
27 | 27 |
|
28 | 28 | ```js |
29 | 29 | let generator = pseudoRandom(1); |
|
0 commit comments