0

My code keeps giving the same results, each for loop repetition always returns same value, which makes scores identical if I try to execute program the same amount of times.

How can I solve my problem?

#include <iostream>
#include <random>

double a=0; //stores instances of a randomized number
double b=0;
double c=0;
double d=0;
int y;

std::random_device rd;
std::uniform_int_distribution<int> dist(0, 3);

void Random(int y){     //function cycles y times and each time gets one value 0-3 and increases a,b,c,d by 1
    for(int i=0;i<y;i++){
    switch(dist(rd)){      //a(0), b(1), c(2), d(3)
case 0:
    a++;
    break;
case 1:
    b++;
    break;
case 2:
    c++;
    break;
case 3:
    d++;
    break;
    }
}
}

int main(){
std::cin>>y;
std::cout<<'\n';
Random(y);      //running function Random
std::cout<<"a: "<<a<<" b: "<<b<<" c: "<<c<<" d: "<<d<<'\n';     //presents how many times we get each value
std::cout<<"a: "<<a/y*100<<"% b: "<<b/y*100<<"% c: "<<c/y*100<<"% d: "<<d/y*100<<"%";       //scores percentage
}

1ST TRY: 1000000

a: 249245 b: 250866 c: 249829 d: 250060 a: 24.9245% b: 25.0866% c: 24.9829% d: 25.006% Process returned 0 (0x0) execution time : 7.603 s

2ND TRY: 1000000

a: 249245 b: 250866 c: 249829 d: 250060 a: 24.9245% b: 25.0866% c: 24.9829% d: 25.006% Process returned 0 (0x0) execution time : 4.792 s

Newbie
  • 9
  • 5

1 Answers1

-1

You cannot use std::random_device like this. It is only intended for seeding a random engine, and there is no guarantee at all on what comes out of it. In fact one implementation of it (the one in mingw) was just return 4 for some time.

Here is how to use it:

// Initial setup
std::random_device seeder;
std::mt19937_64 generator(seeder());
std::uniform_int_distribution<int> between0and3(0, 3);

// Then use for generating numbers
int x = between0and3(generator);

Do not repeat the initial setup steps, if you need more than one number keep the generator and distribution around for subsequent calls.

If you are indeed using an old version of mingw this will not be enough to solve your issue as seeder() will return the same value everytime. In that case you should upgrade it. If not possible, xor-ing the seed with it some other source of variation - current timer or OS-provided random number could be a workaround.

spectras
  • 13,105
  • 2
  • 31
  • 53
  • 2
    Please don't use `std::default_random_engine`, you don't know what you'll get (it could be as bad as a linear congruential generator). Better be specific and use the engine you want - like `std::mt19937_64` or a similar sane choice. And btw; if the "seeder" is deterministic, then so will the output of the engine be - better mix in a few other seed values as well, like time, PID, UID etc. – Jesper Juhl Oct 05 '22 at 17:17
  • This is factually incorrect. `std::random_device` is _supposed_ to be a perfectly-usable RNG in any context. The problem is that its specification is so monumentally broken that implementations are basically allowed to not work. (And even crash.) – Dúthomhas Oct 05 '22 at 17:19
  • LOL, [link to xkcd](https://xkcd.com/221/)? The specification, poor as it is, does _not_ permit `return 4` as valid. – Dúthomhas Oct 05 '22 at 17:24
  • 1
    That xkcd is inspired by a real case - mingw64 did return a hardcoded number - see its bug report #338. – spectras Oct 05 '22 at 17:26
  • I have done so (both options) and numbers still are the same for exact for loop repetition just different values :/ – Newbie Oct 05 '22 at 17:28
  • @Newbie> are you using mingw64 by any chance? – spectras Oct 05 '22 at 17:28
  • @spectras I am using mingw64 :) – Newbie Oct 05 '22 at 17:34
  • @Newbie your best bet is probably to upgrade it to a newer version then. You could work around the problem by mixing another source of variation when seeding the generator, but as the issue will be fixed by just upgrading, that is probably the best course of action. – spectras Oct 05 '22 at 17:38
  • 1
    @spectras Tried running the same code on "Coding C++" android app and suprisingly enough it worked just fine. So I need to change the compiling software on my pc I guess. Thanks for your help ;) – Newbie Oct 05 '22 at 17:50