Floats rounding up

I made this dice simulator which basically throws the dice 1 million times and outputs the frequency and percentage average for each side (1 to 6).

Everything is working fine, except my averages (floats) seem to be rounding up, causing 4% being "unassigned" at the end of the million rolls. I'm outputting with a setprecision of 2, but I only get 0's and no fractional numbers.

Is there anyway to work around this issue ?


Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <iomanip>

int main()
{
    const int counts[] = { 166652, 166675, 166668, 166663, 166678, 166664 } ;

    std::cout << std::fixed << std::setprecision(2) ;

    for( int v : counts )
    {
        const double pct = ( v * 100 ) / 1000000 ; // integer division
        std::cout << pct << ' ' ;
    }
    std::cout << '\n' ;
    // prints: 16.00 16.00 16.00 16.00 16.00 16.00

    for( int v : counts )
    {
        const double pct = ( v * 100.0 ) / 1000000 ;
        std::cout << pct << ' ' ;
    }
    std::cout << '\n' ;
    // prints: 16.67 16.67 16.67 16.67 16.67 16.67
}

http://ideone.com/IYSDXk
Paraphrasing a famous adage "A picture is worth a Thousand Words" can be said about your code it's better to see how it looks.So bring it on forum.
I was able to get a result of 16.67% after correcting an int to float conversion issue. I'm troubled a little bit by the fact that my program seems more accurate than my teacher's example, which has results ranging from 16.50% to 16.90%. Could it be that my randomization is flawed or that mine is in fact more truly random ?

Anyway, here's the part of the code involving it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
for(Nb = 1; Nb >= 1 && Nb <= 1000000; Nb++)
        {
                // Calcul
                srand(time(NULL) + Nb);
                Face = rand() % 6 + 1;
 
                switch(Face)
                {
                        case 1: Fq1++;
                                        break;
 
                        case 2: Fq2++;
                                        break;
 
                        case 3: Fq3++;
                                        break;
 
                        case 4: Fq4++;
                                        break;
 
                        case 5: Fq5++;
                                        break;
 
                        case 6: Fq6++;
                                        break;
                }
 
 
               
                float Pour1 = Fq1 * 100 / Nb;                                                                                                          
                float Pour2 = Fq2 * 100 / Nb;                                                                                                          
                float Pour3 = Fq3 * 100 / Nb;                                                                                                          
                float Pour4 = Fq4 * 100 / Nb;                                                                                                          
                float Pour5 = Fq5 * 100 / Nb;                                                                                                          
                float Pour6 = Fq6 * 100 / Nb;                                                                                                          
               
 
 
                // Affichage
                if(Nb % Mod == 0)
                {
                        gotoxy(0,1);
                        cout << "Essaie avec " << Nb << " lancer(s):";
                        cout << endl << endl;
                        cout << setw(L1) << right << "Face";
                        cout << setw(L2) << right << "Fr\x82quence";
                        cout << setw(L3) << right << "Pourcentage";
                        cout << endl;
                        cout << setw(L1) << right << "1";                                                                                                      
                        cout << setw(L2) << right << Fq1;                                                                                                      
                        cout << fixed << setprecision(2) << setw(L3) << right << Pour1 << "%";                         
                        cout << endl;
                        cout << setw(L1) << right << "2";                                                                                                      
                        cout << setw(L2) << right << Fq2;                                                                                                                                                                                                      
                        cout << setw(L3) << right << Pour2 << "%";                                                                                     
                        cout << endl;
                        cout << setw(L1) << right << "3";                                                                                                      
                        cout << setw(L2) << right << Fq3;                                                                                                      
                        cout << setw(L3) << right << Pour3 << "%";                                                                                     
                        cout << endl;
                        cout << setw(L1) << right << "4";                                                                                                      
                        cout << setw(L2) << right << Fq4;                                                                                                      
                        cout << setw(L3) << right << Pour4 << "%";                                                                                     
                        cout << endl;
                        cout << setw(L1) << right << "5";                                                                                                      
                        cout << setw(L2) << right << Fq5;                                                                                                      
                        cout << setw(L3) << right << Pour5 << "%";                                                                                     
                        cout << endl;
                        cout << setw(L1) << right << "6";                                                                                                      
                        cout << setw(L2) << right << Fq6;                                                                                                      
                        cout << setw(L3) << right << Pour6 << "%";                                                                                     
                        cout << endl << endl;
                       
                        if(Nb == 1000000)
                        {
                                cout << "Popup \x85 venir.";
                                _getch();
                        }
                        else
                        {
                                Mod = Mod * 10;
 
                                string Cont = "Presser une touche pour continuer.";
                                Centre = Cont.size();
 
                                cout << setw((80 - Centre) / 2) <<"" << Cont;
                                _getch();
                                clrscr();
                        }
                }
        }
}


Do not evaluate the last if, it is unfinished and will only be useful to output a goodbye message once the program has successfully shown the results for 1, 10, 100, 1000, 10000, 100000, 1000000 rolls (every power of 10, up to 1 million).
you should use rand() once before each call to it because the first answer is flawed, like you you should use it once just after your call to srand(), and your function would produce better results!
Last edited on
Alright thank you very much I'll add it to my code in the morning !

EDIT: Well it turns out the only problem was that I didn't realize my srand() was inside my for loop, resulting in tons of seed duplicates.

Thanks again for the tip !
Last edited on
Topic archived. No new replies allowed.