Simulation coin Toss (C language implementation)

Source: Internet
Author: User

Implementation code:

1#include <stdio.h>2#include <stdlib.h>3 4 intheads ()5 {6     returnRand () < rand_max/2;7 }8 9 intMainintargcChar*argv[])Ten { One     inti,j,cnt; A     intN = Atoi (argv[1]), M = Atoi (argv[2]); -     int*f = malloc ((n+1)*sizeof(int)); -      the      for(j=0; j<=n; J + +) F[j] =0; -      for(i=0; i<m; i++, f[cnt]++) -        for(cnt=0, j=0; j<n; J + +)//j<=n on book. -         if(Heads ()) cnt++; +          -      for(j=0; j<=n; J + +) +     { Aprintf"%2d", j); at         for(i=0; i<f[j]; i+=Ten) printf ("*"); -printf"\ n"); -     } -                -System"Pause"); -     return 0; in}

The program is from the algorithm: C language Implementation (part 1th to 4th), the main learning is based on the computed value as an array index operation.

The 17th line of code in the comments in order to make the changes, the original book is j<=n, I think it should be j<n, otherwise if the return value of heads () has always been true that the coin toss result is always positive, then the value of the parameter CNT final result is n+1, exceeding the maximum possible number of occurrences.

Save the code as COIN.C, compile the build coin.exe. Assuming that the simulation 1000 times the "toss 32 times", namely n=32,m=1000, through the command line to the main () function pass these two parameters and execute, the result is as follows:

D:\>coin.exe
0
1
2
3
4
5
6 *
7 *
8 *
9 *
Ten * *
* * *
******
********
************
*************
**************
*************
*************
********
******
* * *
*
*
*
*
-
-
-
in
-
to
+
Please press any key to continue ...

Each asterisk in the figure represents 10 occurrences of the front.

Suppose the simulation 10,000 times "toss 32 times", that is, n=32,m=10000, and the code in the 23rd line i+=10 to i+=20 after recompiling the generated coin.exe, through the command line to the main () function passed n, M two parameters and executed, the result is as follows:

D:\>coin.exe 32 10000
0
1
2
3
4
5
6 *
7 *
8 * *
9 * * *
10 *********
11 ***************
12 ****************************
13 ****************************************
14 *****************************************************
15 ********************************************************************
16 ***************************************************************************
17 ******************************************************************
18 *********************************************************
19 ****************************************
20 **************************
21 *************
22 ********
23 * * *
24 *
25 *
26 *
27
28 *
29
30
31
32
Please press any key to continue ...

References: algorithm: C language Implementation (part 1th to 4th), mechanical industry press, 2011.8

Simulation coin Toss (C language implementation)

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.