Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* simulacao de experiencias aleatorias lancamento de um dado: quantas vezes, k, sai a face 6 em n lancamentos, por forma a obter a frequencia de saida, k/n; exemplo: 0.02 */
- // Apple Xcode
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- int main (int argc, const char * argv[])
- {
- int the_k = 0;
- int the_n = 0;
- int the_face = 0;
- float the_freq = 0;
- float the_desv = 0;
- float the_tol = 0;
- printf("tolerancia ]0, 0.17]: ");
- scanf("%f", &the_tol);
- if (the_tol > 0 && the_tol <= 0.17)
- {
- while (1)
- {
- the_face = 1 + rand() % 6;
- the_n++; // lancamento
- if (the_face == 6)
- {
- the_k++; // mais uma face 6
- printf("%d faces 6 em %d lancamento\n", the_k, the_n);
- the_freq = (float) the_k / the_n;
- the_desv = fabs(the_freq - 1 / 6.0); // fabs: valor abs de reais
- printf("freq = %f\n", the_freq);
- printf("desvio = %f\n", the_desv);
- if (the_desv < the_tol) break;
- }
- }
- }
- else
- {
- printf("]0, 0.17]");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement