refactor promise, geneerators, global object
This commit is contained in:
parent
be9c5a7b5f
commit
2ee2751216
69 changed files with 900 additions and 643 deletions
|
@ -0,0 +1,9 @@
|
|||
function* pseudoRandom(seed) {
|
||||
let value = seed;
|
||||
|
||||
while(true) {
|
||||
value = value * 16807 % 2147483647
|
||||
yield value;
|
||||
}
|
||||
|
||||
};
|
|
@ -0,0 +1,21 @@
|
|||
describe("pseudoRandom", function() {
|
||||
|
||||
it("follows the formula", function() {
|
||||
let generator = pseudoRandom(1);
|
||||
|
||||
assert.equal(generator.next().value, 16807);
|
||||
assert.equal(generator.next().value, 282475249);
|
||||
assert.equal(generator.next().value, 1622650073);
|
||||
});
|
||||
|
||||
|
||||
it("returns same value for the same seed", function() {
|
||||
let generator1 = pseudoRandom(123);
|
||||
let generator2 = pseudoRandom(123);
|
||||
|
||||
assert.deepEqual(generator1.next(), generator2.next());
|
||||
assert.deepEqual(generator1.next(), generator2.next());
|
||||
assert.deepEqual(generator1.next(), generator2.next());
|
||||
});
|
||||
|
||||
});
|
|
@ -0,0 +1,38 @@
|
|||
```js run demo
|
||||
function* pseudoRandom(seed) {
|
||||
let value = seed;
|
||||
|
||||
while(true) {
|
||||
value = value * 16807 % 2147483647
|
||||
yield value;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
let generator = pseudoRandom(1);
|
||||
|
||||
alert(generator.next().value); // 16807
|
||||
alert(generator.next().value); // 282475249
|
||||
alert(generator.next().value); // 1622650073
|
||||
```
|
||||
|
||||
Please note, the same can be done with a regular function, like this:
|
||||
|
||||
```js run
|
||||
function pseudoRandom(seed) {
|
||||
let value = seed;
|
||||
|
||||
return function() {
|
||||
value = value * 16807 % 2147483647;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
let generator = pseudoRandom(1);
|
||||
|
||||
alert(generator()); // 16807
|
||||
alert(generator()); // 282475249
|
||||
alert(generator()); // 1622650073
|
||||
```
|
||||
|
||||
That's fine for this context. But then we loose ability to iterate with `for..of` and to use generator composition, that may be useful elsewhere.
|
|
@ -0,0 +1,34 @@
|
|||
|
||||
# Pseudo-random generator
|
||||
|
||||
There are many areas where we need random data.
|
||||
|
||||
One of them is testing. We may need random data: text, numbers etc, to test things out well.
|
||||
|
||||
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.
|
||||
|
||||
For that, so called "seeded pseudo-random generators" are used. They take a "seed", the first value, and then generate next ones using a formula. So that the same seed yields the same sequence, and hence the whole flow is easily reproducable. We only need to remember the seed to repeat it.
|
||||
|
||||
An example of such formula, that generates somewhat uniformly distributed values:
|
||||
|
||||
```
|
||||
next = previous * 16807 % 2147483647
|
||||
```
|
||||
|
||||
If we use `1` as the seed, the values will be:
|
||||
1. `16807`
|
||||
2. `282475249`
|
||||
3. `1622650073`
|
||||
4. ...and so on...
|
||||
|
||||
The task is to create a generator function `pseudoRandom(seed)` that takes `seed` and creates the generator with this formula.
|
||||
|
||||
Usage example:
|
||||
|
||||
```js
|
||||
let generator = pseudoRandom(1);
|
||||
|
||||
alert(generator.next().value); // 16807
|
||||
alert(generator.next().value); // 282475249
|
||||
alert(generator.next().value); // 1622650073
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue