Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Generators#414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
function* pseudoRandom(seed) {
letvalue =seed;
function* pseudoRandom(semente) {
letvalor =semente;

while(true) {
value =value * 16807 % 2147483647
yieldvalue;
valor =valor * 16807 % 2147483647
yieldvalor;
}

};
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
describe("pseudoRandom", function() {

it("follows the formula", function() {
it("segue a fórmula", function() {
let generator = pseudoRandom(1);

assert.equal(generator.next().value, 16807);
Expand All@@ -9,7 +9,7 @@ describe("pseudoRandom", function() {
});


it("returns same value for the same seed", function() {
it("retorna o mesmo valor para a mesma semente", function() {
let generator1 = pseudoRandom(123);
let generator2 = pseudoRandom(123);

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,7 +16,7 @@ alert(generator.next().value); // 282475249
alert(generator.next().value); // 1622650073
```

Please note, the same can be done with a regular function, like this:
Observe que o mesmo pode ser feito com uma função regular, assim:

```js run
function pseudoRandom(seed) {
Expand All@@ -35,4 +35,4 @@ alert(generator()); // 282475249
alert(generator()); // 1622650073
```

That also works. But then we lose ability to iterate with `for..of`and to use generator composition, that may be useful elsewhere.
Isso também funciona. No entanto, perdemos a capacidade de iterar com `for..of`e de usar a composição de geradores, o que pode ser útil em outros contextos.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@

#Pseudo-random generator
#Gerador pseudoaleatório

There are many areas where we need random data.
Existem muitas áreas onde precisamos de dados aleatórios.

One of them is testing. We may need random data: text, numbers, etc.to test things out well.
Uma delas é em testes. Podemos precisar de dados aleatórios: texto, números, etc.para testar as coisas adequadamente.

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.
Em JavaScript,poderíamos usar`Math.random()`.Mas se algo der errado, gostaríamos de poder repetir o teste, usando exatamente os mesmos dados.

For that, so called "seeded pseudo-random generators" are used. They take a "seed",the first value, and then generate the next ones using aformula 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.
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 afó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.

An example of such formula, that generates somewhat uniformly distributed values:
Um exemplo de tal fórmula, que gera valores distribuídos de maneira um tanto uniforme:

```
next = previous * 16807 % 2147483647
```

If we use`1`as the seed, the values will be:
Se usarmos`1`como semente, os valores serão:
1. `16807`
2. `282475249`
3. `1622650073`
4. ...and so on...
4. ...e assim por diante...

The task is to create a generator function`pseudoRandom(seed)`that takes `seed` and creates the generator with this formula.
A tarefa é criar uma função geradora`pseudoRandom(semente)`que recebe uma `semente` e cria o gerador com esta fórmula.

Usage example:
Exemplo de uso:

```js
let generator = pseudoRandom(1);
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp