mercredi 5 octobre 2022

RNG - getting same results with each repetition (cpp)

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




Aucun commentaire:

Enregistrer un commentaire