|
1 | 1 |
|
2 | | -# Pseudo-random generator |
| 2 | +# Gerador pseudoaleatório |
3 | 3 |
|
4 | | -There are many areas where we need random data. |
| 4 | +Existem muitas áreas onde precisamos de dados aleatórios. |
5 | 5 |
|
6 | | -One of them is testing. We may need random data: text, numbers, etc. to test things out well. |
| 6 | +Uma delas é em testes. Podemos precisar de dados aleatórios: texto, números, etc. para testar as coisas adequadamente. |
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 | +Em JavaScript, poderíamos usar `Math.random()`. Mas se algo der errado, gostaríamos de poder repetir o teste, usando exatamente os mesmos dados. |
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 isso, são usados os chamados "geradores pseudoaleatórios com semente". Eles recebem uma "semente", o primeiro valor, e em seguida gera os próximos usando a fórmula para que a mesma semente produza a mesma sequência, tornando todo o fluxo fácil de se reproduzir. Precisamos apenas lembrar da semente para repeti-lo. |
11 | 11 |
|
12 | | -An example of such formula, that generates somewhat uniformly distributed values: |
| 12 | +Um exemplo de tal fórmula, que gera valores distribuídos de maneira um tanto 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 | +Se usarmos `1` como semente, os valores serão: |
19 | 19 | 1. `16807` |
20 | 20 | 2. `282475249` |
21 | 21 | 3. `1622650073` |
22 | | -4. ...and so on... |
| 22 | +4. ...e assim por diante... |
23 | 23 |
|
24 | | -The task is to create a generator function `pseudoRandom(seed)` that takes `seed` and creates the generator with this formula. |
| 24 | +A tarefa é criar uma função geradora `pseudoRandom(semente)` que recebe uma `semente` e cria o gerador com esta fórmula. |
25 | 25 |
|
26 | | -Usage example: |
| 26 | +Exemplo de uso: |
27 | 27 |
|
28 | 28 | ```js |
29 | 29 | let generator = pseudoRandom(1); |
|
0 commit comments