i trying work out how make dice rolling application display percentage chance each result. example single 6 sided dice display: 1 = 16.6% 2 = 16.6% 3 = 16.6% 4= 16.6% 5 = 16.6% 6 = 16.6%.
the user input type of dice (number of sides n) , number of dice rolled (d).
has got formula work c++? can't seem work out/understand have been able find far.
thanks
edit store results in array of sort if possible
void dice_analysis(int dice_sides, int dice_number, float * h2, int precision) { int temp=0; int histogram[dice_sides*dice_number]; //init histogram for(int k=0;k<dice_sides*dice_number;k++) { histogram[k]=0; } //statistical analysis for(int i=0;i<precision;i++) { temp=0; for(int j=0;j<dice_number;j++) { temp+=( rand()%dice_sides +1 ); } for(int k=0;k<dice_sides*dice_number;k++) { if(temp==(k+1)){histogram[k]++;} //actually histogram[temp]++; better } } //printing calculated results for(int k=0;k<dice_sides*dice_number;k++) { cout<<k+1<<":"<<(float)histogram[k]/(precision/100.0f)<<"%"<<endl; h2[k]=histogram[k]/(precision/100.0f); } } output dice_analysis(8,2,x,1000) ---> 2 8 sided dices
1:0% 2:1.5% 3:4.4% 4:3.8% 5:5.7% 6:8.8% 7:9.1% 8:10.2% 9:14% 10:10.2% 11:8.3% 12:7.5% 13:6.8% 14:6% 15:2.8% 16:0.9% you can see "1" not possible 2 or more dices. "9" probable being %14 chance.
you can use like: dice_analysis(8,2,output_array,1000);
Comments
Post a Comment