Basic algorithm of C language 28-coin toss (random number)

Source: Internet
Author: User

Coin throw (random number)
/*
=====================================================================
Title: Cast two coins, try to analyze the total number of cases, the probability of the difference?
=====================================================================
*/


#include <stdio.h>
#include <iomanip>
#include <ctime>
Main ()
{
int a,b,c,n,x,y;a=0;b=0;c=0;
Srand (Time (0));//This must have, otherwise the random number cannot be generated!
for (n=0;n<100;n++)
{
X=rand ()% (2-0) +0;//produces 0, 12 results, indicating a positive and inverse, so that a random number is generated within [0,2], that is 2=2-0+0, in [A, b] produces a random number: rand ()% (b-a) +a, here a=0,b=2;
Y=rand ()% (2-0) +0;
printf ("%3d:%d,%d", n+1,x,y);
if ((n+1)%5==0) printf ("\ n");
if (x+y==0)
a++;
else if (x+y==1)
b++;
Else
C + +;
}
printf ("Two front:%d, one positive and one counter:%d, two tails:%d\n", a,b,c);
}


/*
====================================================================
Evaluation:

The first thing to know is that this is a combination of C and C + +, in fact they are a language. Must use <ctime> and <iomanip>

function (if you do not know, then the back will be remembered). The test center is to throw two coins can produce three kinds of results: 1, two

Positive, 2, two counter, 3, a positive and reverse, respectively, with 0,1,2, this analysis of two elements need to produce three results, since

It is 0+0=0;0+1=1;1+1=2, so we have to randomly produce 0, 12 elements. That is, a random number is generated in [0,2] or equivalent to

Random number of [0,1]!

=====================================================================
*/






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Basic algorithm of C language 28-coin toss (random number)

Related Article

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.