I was trying to check how random numbers are actually random, but then I encountered a problem in which the line of code v.procs_counter++;
for some reason changes the object’s variable only once
#include <iostream>
#include <random>
#include <windows.h>
class Random {
public:
std::string id;
int min_value, max_value;
int procs_counter = 0;
Random (int minv, int maxv, std::string s) {
this->min_value = minv;
this->max_value = maxv;
this->id = s;
}
};
int main() {
int global_min_value = 0;
int global_max_value = 100;
std::random_device dev;
std::mt19937_64 rng(dev());
std::uniform_int_distribution<std::mt19937_64::result_type> RNGrange(global_min_value, global_max_value);
Random r1(1, 30, "30%");
Random r2(31, 100, "70%");
auto arr = {r1, r2};
int total_attempts = 0;
while (true) {
total_attempts++;
int RNGcheck = RNGrange(rng);
for (auto v: arr) {
if (RNGcheck >= v.min_value & RNGcheck <= v.max_value) {
v.procs_counter++;
std::cout << v.id << "'s average attempts: " << v.procs_counter * 100/total_attempts << std::endl;
}
}
Sleep(60);
}
return 0;
}
I’ve tried doing += 1
instead of ++
and also tried doing v.procs_counter = v.procs_counter + 1
and nothing helped me. It’s always 1 * 100/total_attempts
instead of number 1 actually changing
New contributor
Sinity is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.