bool SimpleDeltaRndNumGenerator::rnd_flipcoin(const unsigned int, const Filter *f, const std::string *where) { int y = random_choice(2, f, where); assert(y == -1 || (y >= 0 && y < 2)); return y; }
unsigned int SimpleDeltaRndNumGenerator::rnd_upto(const unsigned int n, const Filter *f, const std::string *where) { int x = random_choice(n, f, where); assert(x == -1 || (x >= 0 && x < static_cast<int>(n))); return x; }
bool DFSRndNumGenerator::rnd_flipcoin(const unsigned int n, const Filter *f, const std::string *where) { vector<int> invalid; int y; if (n == 100) { invalid.push_back(0); y = random_choice(2, f, where, &invalid); } else if (n == 0) { invalid.push_back(1); y = random_choice(2, f, where, &invalid); } else { y = random_choice(2, f, where); } assert(y == -1 || (y >= 0 && y < 2)); return y; }
std::string SimpleDeltaRndNumGenerator::RandomHexDigits( int num ) { std::string str; const char* hex1 = AbsRndNumGenerator::get_hex1(); while (num--) { int x = random_choice(16, NULL, NULL); str += hex1[x]; } return str; }
std::string SimpleDeltaRndNumGenerator::RandomDigits( int num ) { std::string str; const char* dec1 = AbsRndNumGenerator::get_dec1(); while ( num-- ) { int x = random_choice(10, NULL, NULL); str += dec1[x]; } return str; }
int main(int argc, char** argv) { std::random_device rd; std::mt19937 rng(rd()); std::array<int, 7> x = { 2, 3, 5, 7, 11, 13, 17}; // choose and print a random element from x 5 times for (int i = 0; i < 5; ++i) std::cout << *random_choice(x.begin(), x.end(), rng) << "\n"; // choose a random sample of 3 elements from x and print them 5 times for (int i = 0; i < 5; ++i) { std::array<int, 3> out; random_sample(x.begin(), x.end(), out.begin(), 3, rng); for (auto& e : out) std::cout << e << " "; std::cout << "\n"; } return 0; }
int main(int argc, char **argv) { static bstate states[N]; static bstate out_states[3 * N]; int num_outstates = 0; int j; int k; int x, y; double sum_p = 0.0; double sum_squared_p = 0.0; int num_legal_final_states; int height; int width; int num_iterations = 100; if (argc < 5) { fprintf(stderr, "Usage: estimate_legal_stratified <height> <width> <num_samples> <seed>\n"); return 1; } height = atoi(argv[1]); width = atoi(argv[2]); num_iterations = atoi(argv[3]); gg_srand(atoi(argv[4])); setwidth(height); for (j = 0; j < num_iterations; j++) { double p = 1.0; for (k = 0; k < N; k++) memcpy(states[k], *startstate(), sizeof(bstate)); for (y = 0; y < width; y++) for (x = 0; x < height; x++) { num_outstates = 0; for (k = 0; k < N; k++) { bstate expanded_states[3]; int num_expanded_states; int m; num_expanded_states = expandstate(states[k], x, expanded_states); for (m = 0; m < num_expanded_states; m++) { memcpy(out_states[num_outstates++], expanded_states[m], sizeof(bstate)); } } p *= (double) num_outstates / (3 * N); for (k = 0; k < N; k++) memcpy(states[k], out_states[random_choice(num_outstates)], sizeof(bstate)); } num_legal_final_states = 0; for (k = 0; k < num_outstates; k++) num_legal_final_states += finalstate(out_states[k]); p *= (double) num_legal_final_states / num_outstates; sum_p += p; sum_squared_p += p * p; if ((j + 1) % 10 == 0) { double std = sqrt(sum_squared_p - sum_p * sum_p / (j + 1)) / j; printf("%d %10.8lg %lg %lg\n", j + 1, sum_p / (j + 1), std, std * sqrt(j)); } } printf("Estimated legal probability: %10.8lg\n", sum_p / num_iterations); printf("Standard deviation: %lg\n", sqrt(sum_squared_p - sum_p * sum_p / num_iterations) / num_iterations); printf("Standard deviation per sample: %lg\n", sqrt((sum_squared_p - sum_p * sum_p / num_iterations) / num_iterations)); return 0; }
std::string random_tld() { static const std::vector<std::string> tlds { ".com", ".net", ".org", ".info", ".test", ".invalid" }; return random_choice(tlds); }