29, Toad's data structure note 29 array of coin toss simulation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.