Skip to content

Generators #25

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 number Diff line number Diff 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:
იგივე შეგვიძლია გავაკეთოთ ჩვეულებრივი ფუნქციითაც:

```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.
მართალია ზემოთ მოცემული კოდი მუშაობს, მაგრამ ჩვენ ვკარგავთ `for..of`-ის გამოყენების შესაძლებლობასა და გენერატორების კომპოზიციას, რაც შეიძლება სხვაგან გამოგვადგეს.
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@

# 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.
ჯავასკრიპტში, შეგვიძლია გამოვიყენოთ `Math.random()`. მაგრამ თუ რამე შეცდომა მოხდა, გვინდა რომ ტესტი გავიმეოროთ იმავე მონაცემებით.

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.
ამისათვის, "seeded pseudo-random გენერატორები" გამოიყენება. ისინი იღევენ "seed"-ს, პირველ მნიშვნელობას და დანარჩენებს აგენერირებენ ფორმულით, და შესაბამისად, იგივე "seed" ყოველ ჯერზე იმავე მიმდევრობას დააბრუნებს. ტესტის გასამეორებლად მხოლოდ "seed"-ის დამახსოვრებაა საჭირო.

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`-ს როგორც "seed"-ს, მაშინ მნიშვნელობები იქნება:
1. `16807`
2. `282475249`
3. `1622650073`
4. ...and so on...
4. ...და ასე შემდეგ...

The task is to create a generator function `pseudoRandom(seed)` that takes `seed` and creates the generator with this formula.
ჩვენი დავალებაა შევქმნათ გენერატორი ფუნქცია `pseudoRandom(seed)` რომელიც არგუმენტად იღებს `seed`-ს და ზემო ფორმულით ქმნის გენერატორს.

Usage example:
გამოყენების მაგალითი:

```js
let generator = pseudoRandom(1);
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy