vendredi 26 février 2016

How to store random values in an array of structs

Ok, so what I am needing to do is simulate 2 die rolls 100 times and store each rolls in an array of structs, then process the sum of the two rolls and record the occurences.

Seems easy enough here what i got:

        #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    #include <iostream>
    #include <iomanip>
    #include <windows.h>

    using namespace std;

    struct dice{
        int dice1;
        int dice2;

    };

    int main( void ){


        int i = 0;
        int r = 0;
        int number [14] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};


        struct dice rolls[ 100 ];

        for(i=0; i < 100; i++){
            rolls[ i ].dice1 = rand() % 6 + 1;
            rolls[ i ].dice2 = rand() % 6 + 1;
            if(rolls[i].dice1 + rolls[i].dice2 == 2){
                number[2]++;
            }else if(rolls[i].dice1 + rolls[i].dice2 == 3){
                number[3]++;
            }else if(rolls[i].dice1 + rolls[i].dice2 == 4){
                number[4]++;
            }else if(rolls[i].dice1 + rolls[i].dice2 == 5){
                number[5]++;
            }else if(rolls[i].dice1 + rolls[i].dice2 == 6){
                number[6]++;
            }else if(rolls[i].dice1 + rolls[i].dice2 == 7){
                number[7]++;
            }else if(rolls[i].dice1 + rolls[i].dice2 == 8){
                number[8]++;
            }else if(rolls[i].dice1 + rolls[i].dice2 == 9){
                number[9]++;
            }else if(rolls[i].dice1 + rolls[i].dice2 == 10){
                number[10]++;
            }else if(rolls[i].dice1 + rolls[i].dice2 == 11){
                number[11]++;
            }else if(rolls[i].dice1 + rolls[i].dice2 == 12){
                number[12]++;
            }

        }


        cout<< setw(21) <<"Dice Result: "<< setw(29) << "Number of Occurence:"<<endl;
        for(i = 0; i < 14; i++){
            cout<< setw(20)<< i << setw(30) <<number[i]<<endl;
            Sleep(100);
        }

        return 0;
    }

What the hell am i missing it seems like its all there but i keep getting a non-random number of occurences? Any help of insight would be appreciated!




Aucun commentaire:

Enregistrer un commentaire