-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_dataset.cpp
More file actions
67 lines (54 loc) · 1.87 KB
/
Copy pathgenerate_dataset.cpp
File metadata and controls
67 lines (54 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <random>
#include <algorithm>
#include <stdexcept>
static std::string luhn_complete(const std::string& fifteen) {
int sum = 0;
for (int i = 0; i < 15; ++i) {
int d = fifteen[i] - '0';
if (i % 2 == 0) {
d *= 2;
if (d > 9) d -= 9;
}
sum += d;
}
return fifteen + char('0' + (10 - (sum % 10)) % 10);
}
static std::vector<std::string> generate_universe(size_t n, uint64_t seed) {
std::mt19937_64 rng(seed);
std::uniform_int_distribution<uint64_t> dist(0, 99999999999999ULL);
std::vector<std::string> universe;
universe.reserve(n);
do{
while (universe.size() < n) {
// 14 random digits padded to 14 chars, prefixed with "4" = 15 digits
std::string suffix = std::to_string(dist(rng));
suffix = std::string(14 - suffix.size(), '0') + suffix;
std::string card = luhn_complete("4" + suffix);
universe.push_back(card);
}
// deduplicate
std::sort(universe.begin(), universe.end());
universe.erase(std::unique(universe.begin(), universe.end()), universe.end());
} while(universe.size() < n);
return universe;
}
int main(int argc, char* argv[]) {
if (argc != 3) {
std::cerr << "Usage: ./generate_dataset <count> <output_file>\n";
std::cerr << "Example: ./generate_dataset 1000 cards.txt\n";
return 1;
}
size_t n = std::stoull(argv[1]);
std::string path = argv[2];
auto universe = generate_universe(n, /*seed=*/42);
std::ofstream out(path);
if (!out) { std::cerr << "Failed to open " << path << "\n"; return 1; }
for (const auto& card : universe)
out << card << "\n";
std::cout << "Generated " << universe.size() << " cards → " << path << "\n";
return 0;
}