29, Toad's data structure note 29 array of coin toss simulation
This famous article:"Life is a variety of different changes, the cycle of pain and joy composed." The eternal Blue sky exists only in the middle of the mind, to the reality of life to ask is wishful thinking. - - Balzac "
Welcome Reprint, reproduced please indicate the source:
1. Coin toss
If you toss a coin n times, you see that the expectation of the Avatar is N/2 times, but the actual value may also be 0~n times, M test in the program, M and n are defined in the code. It uses an array f to track the probability of an "I-second Avatar", where 0≤j≤n. The bar chart of the test results is then printed, which is represented by 1 asterisks, each appearing 10 times.
2. Code
Code execution is shown in 1, and 0-9 means that 10 coins are executed at a time.
Behind the Mimei said, every 10 times the total number of coins to get a head.
Indicates a total of 100 matches, 10 throws per field. You can see that 10 of the coins have the highest number of times 6 and 5, in line with the phenomenon of normal distribution.
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
void Main (intargc, Char* argv[])
{
inti,j,cnt,k;
int n=10;
int m=100;
doublef[10]={0,0,0,0,0,0,0,0,0,0};
Srand (Time (NULL));
for (i=0;i<m;i++,f[cnt]=f[cnt]+1)
for (cnt=0,j=0;j<=n;j++)
{
k= rand () +1;
k= k% 10;
if (K < 5)
cnt++;
}
for (j=0;j<n;j++)
{
printf ("%2d", j+1);
for (i=0;i<f[j];i++)
printf ("*");
printf ("\ n");
}
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
29, Toad's data structure note 29 array of coin toss simulation