Reputation: 13
I have a nested system as described in the pseudocode below (part of a random weighted majority algorithm):
function1() {
//for 100 iterations:
function2()
// grab logistics
}
function2() {
// create a random seed/generator
random_device rd;
mt19937 gen(rd);
//for 1000 iterations:
function3(gen);
}
function3(gen) {
// grab number from uniform_real_distribution using gen
// then use that number against differing weights
// such that higher weight gets more territory in the uniform distribution for its desired outcome
// that is in a system with weights (1, 1/2) distributed over a uniform distribution (0,1)
// outcome of weight 1 happens if dist lands (0,.6666) and outcome of weight 2 if dist lands (.6666, 1)
}
In the above example, uniform_real_distribution generates what appears to be random numbers, but function1 always ends up with the exact same result.
However, when I run this function1 will always get the same exact results in every iteration, even though the other two functions are supposed to be random. Even worse, if I change the generator from something like mt19937 to ranlux48, the system will get the exact same results each iteration, but that exact result will be different from the one the mt19937 got, which means everything I'm doing is not random-- only dependent on the generator.
I need guidance on how to fix this such that I have truly random results.
Where should I place gen and rd? Should I even use a uniform real distribution?
If I create gen in function3 every time it is called, I also still get non-random results, in fact uniform_real_distribution generates the exact same value every time
Upvotes: 0
Views: 79
Reputation: 13
As user4581301 pointed out in the comments I was using an old version of MinGW in which random_device was broken, producing the same seed every time. Consequently, because of the scope of my random generator I would start on the same seed and get the same result every time. It's not actually a program issue, but a compiler one, which is why I was confused!
Upvotes: 0
Reputation: 60228
Although you have only shown pseudocode, it appears that you are creating a new random device and generator every time you call the function. This is needlessly expensive, and more importantly, every time you call the function, you'll get the same results from the random generator. The simplest modification to your pseudocode would be to make the generator static, like this:
function2() {
// create a random seed/generator ONCE only
static random_device rd;
static mt19937 gen(rd);
// work
Upvotes: 1