# generator

***

**1. Random Number Generation:**

```cpp
#include <random>

std::random_device rd;
std::default_random_engine gen(rd());
std::uniform_int_distribution<int> dist(1, 10);

for (int i = 0; i < 10; i++) {
  int num = dist(gen);  // Generate a random number between 1 and 10
}
```

**2. Password Generation:**

```cpp
#include <random>
#include <string>

std::random_device rd;
std::default_random_engine gen(rd());
std::uniform_int_distribution<int> charDist('a', 'z');  // Generate lowercase letters

std::string generatePassword(int length) {
  std::string password;
  for (int i = 0; i < length; i++) {
    char c = charDist(gen);
    password.push_back(c);
  }
  return password;
}
```

**3. String Permutation Generation:**

```cpp
#include <algorithm>
#include <iostream>

std::string str = "abc";

// Use a generator function to generate permutations
std::function<std::string()> perm = [&str] {
  std::random_shuffle(str.begin(), str.end());
  return str;
};

for (int i = 0; i < 10; i++) {
  std::cout << perm() << std::endl;  // Print a random permutation of "abc"
}
```

**4. Graph Traversal (Breadth-First Search):**

```cpp
#include <queue>
#include <vector>

// Represent a graph as a vector of vectors
std::vector<std::vector<int>> graph = {
  {1, 2},
  {0, 2},
  {0, 1, 3},
  {2}
};

// BFS using a generator function
std::function<int()> bfs = [&graph] {
  std::queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();  // Get the current vertex
    q.pop();
    for (int w : graph[v]) {
      q.push(w);  // Add the neighbors to the queue
    }
    return v;  // Return the visited vertex
  }
};

for (int i = 0; i < graph.size(); i++) {
  std::cout << bfs() << std::endl;  // Visit the vertices in BFS order
}
```

**5. FizzBuzz (Generator Expression):**

```cpp
#include <iostream>

auto fizzbuzz = [](int n) {
  for (int i = 1; i <= n; i++) {
    if (i % 3 == 0 && i % 5 == 0) {
      std::cout << "FizzBuzz" << std::endl;
    } else if (i % 3 == 0) {
      std::cout << "Fizz" << std::endl;
    } else if (i % 5 == 0) {
      std::cout << "Buzz" << std::endl;
    } else {
      std::cout << i << std::endl;
    }
  }
};

for (auto& num : fizzbuzz(15)) {
  std::cout << num << " ";
}
```

**6. Fibonacci Sequence:**

```cpp
#include <vector>

std::function<int()> fibonacci = [] {
  int a = 0, b = 1;
  while (true) {
    int c = a + b;
    a = b;
    b = c;
    yield return a;  // Return the next Fibonacci number using the yield keyword
  }
};

for (int i : fibonacci()) {
  if (i > 100) break;
  std::cout << i << " ";
}
```

**7. Infinite Loop with Yield:**

```cpp
#include <iostream>

std::function<void()> infiniteLoop = [] {
  while (true) {
    std::cout << "This is an infinite loop." << std::endl;
    yield;
  }
};

for (int i = 0; i < 5; i++) {
  infiniteLoop();
}
```

**8. Range Generator:**

```cpp
#include <iostream>

std::function<int()> range = [](int a, int b) {
  for (int i = a; i <= b; i++) {
    yield return i;  // Yield the next number in the range
  }
};

for (int i : range(1, 10)) {
  std::cout << i << " ";
}
```

**9. Multiples of a Number:**

```cpp
#include <iostream>

std::function<int()> multiples = [](int n) {
  for (int i = 0;; i++) {  // Use an infinite for loop to generate an infinite sequence
    yield return n * i;
  }
};

for (int i : multiples(5)) {
  if (i > 100) break;
  std::cout << i << " ";
}
```

**10. Unique Random Numbers:**

```cpp
#include <random>
#include <unordered_set>

std::function<int()> uniqueRandom = [] {
  std::unordered_set<int> used;
  std::random_device rd;
  std::default_random_engine gen(rd());
  std::uniform_int_distribution<int> dist(1, 100);
  while (true) {
    int num = dist(gen);
    if (used.find(num) == used.end()) {
      used.insert(num);
      yield return num;
    }
  }
};

for (int i : uniqueRandom()) {
  if (i > 50) break;
  std::cout << i << " ";
}
```

**11. Zip Two Sequences (Generator Expression):**

```cpp
#include <iostream>
#include <vector>

std::function<std::pair<int, int>()> zip = [](std::vector<int>& a, std::vector<int>& b) {
  for (int i = 0; i < a.size() && i < b.size(); i++) {
    yield return std::make_pair(a[i], b[i]);
  }
};

std::vector<int> v1 = {1, 2, 3};
std::vector<int> v2 = {4, 5, 6};

for (auto& [num1, num2] : zip(v1, v2)) {
  std::cout << num1 << " " << num2 << std::endl;
}
```

**12. Iterate Over a Map:**

```cpp
#include <iostream>
#include <map>

std::function<std::pair<std::string, int>()> iterateMap = [](std::map<std::string, int>& m) {
  for (auto it = m.begin(); it != m.end(); it++) {
    yield return std::make_pair(it->first, it->second);
  }
};

std::map<std::string, int> m = {{"apple", 1}, {"banana", 2}, {"cherry", 3}};

for (auto& [key, value] : iterateMap(m)) {
  std::cout << key << ": " << value << std::endl;
}
```

**13. Prime Number Generator:**

```cpp
#include <iostream>

std::function<int()> primeGenerator = [] {
  int num = 2;
  while (true) {
    bool isPrime = true;
    for (int i = 2; i <= std::sqrt(num); i++) {
      if (num % i == 0) {
        isPrime = false;
        break;
      }
    }
    if (isPrime) yield return num;
    num++;
  }
};

for (int i : primeGenerator()) {
  if (i > 100) break;
  std::cout << i << " ";
}
```

**14. Random Colors (RGB):**

```cpp
#include <random>
#include <iostream>

std::function<std::tuple<int, int, int>()> randomColor = [] {
  std::random_device rd;
  std::default_random_engine gen(rd());
  std::uniform_int_distribution<int> dist(0, 255);
  while (true) {
    int r = dist(gen);
    int g = dist(gen);
    int b = dist(gen);
    yield return std::make_tuple(r, g, b);
  }
};

for (auto& [r, g, b] : randomColor()) {
  std::cout << "RGB(" << r << ", " << g << ", " << b << ")" << std::endl;
}
```

**15. Eratosthenes Prime Sieve:**

```cpp
#include <vector>

std::function<std::vector<int>()> eratosthenes = [](int n) {
  std::vector<bool> sieve(n + 1, true);
  for (int i = 2; i * i <= n; i++) {
    if (sieve[i]) {
      for (int j = i * i; j <= n; j += i) {
        sieve[j] = false;
      }
    }
  }
  std::vector<int> primes;
  for (int i = 2; i <= n; i++) {
    if (sieve[i]) yield return i;
  }
};

for (int prime : eratosthenes(100)) {
  std::cout << prime << " ";
}
```

**16. Coroutine to Read from a File:**

```cpp
#include <experimental/coroutine>
#include <fstream>
#include <iostream>

using namespace std::experimental;

struct FileGenerator {
  std::fstream file;
  FileGenerator(const std::string& filename) : file(filename, std::ios::in) {}
  std::suspend_always operator()() {
    std::string line;

```
