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)