Randomization algorithm (2)-numerical probability algorithm

Source: Internet
Author: User

Next article: randomization algorithm (1)-Random Number

We recommend the random algorithm summarized by chinazhangjie at the beginning of this chapter. Because we read the same book, you can also refer to it. The summary is very good.

Http://www.cnblogs.com/chinazhangjie/archive/2010/11/11/1874924.html

(By The Way, Xiao jie is also the moderator of the C/C ++ issue help section of my forum, and the C/C ++ mavericks)

In this chapter, I will give an example in the book. Although it is very simple, it is very interesting.

Calculate Pi value using random Projection Method

A circle with a radius of r and its external tangent quadrilateral are provided. Randomly throw n points to the square. Set the number of points falling into the circle to k. Because the input points are evenly distributed on the square, the probability of the Input points falling into the circle is (Pi * r)/(4 * r) = Pi/4. So when n is large enough, the ratio of k to n approaches this probability. Thus, PI is approximately equal to (4 * k)/n.

For example:

 

Because the RandomNumber class in the previous chapter probability algorithm (1)-random number is used in the code, you can take a look at the previous chapter first.

I put thisPseudo-Random classPaste it again:

Const unsigned long maxshort = 65535L;

Const unsigned long multiplier = 1194211693l;
Const unsigned long adder = 12345l;
 
Class randomnumber {
PRIVATE:
// Current seed
Unsigned long randseed;
Public:
// Constructor. The default value 0 indicates that the seed is automatically generated by the system.
Randomnumber (unsigned long s = 0 );
// Generate 0 ~ Random integer between n-1
Unsigned short random (unsigned long N );
// Generate a random real number between 0 and 1
Double frandom ();
};
 
// Generate Seeds
RandomNumber: RandomNumber (unsigned long s)
{
If (s = 0)
RandSeed = time (0); // seed generated by system time
Else
RandSeed = s;
}
 
// Generate 0 ~ Random integer between n-1
Unsigned short RandomNumber: Random (unsigned long n)
{
RandSeed = multiplier * randSeed + adder;
Return (unsigned short) (randSeed> 16) % n );
}
 
// Generate a random real number between 0 and 1
Double RandomNumber: fRandom ()
{
Return random (maxshort)/double (maxshort );
}

Main file:

/*
* Author: tanky Woo
* Blog: www.wutianqi.com
* Date: 2010.12.8
* Calculate pi value using random Projection Method
* The Code comes to Wang Xiaodong's computer algorithm design and analysis
*/
 
# Include "randomnumber. H"
# Include <iostream>
# Include <iomanip>
# Include <time. h>
Using namespace STD;
 
Double darts (long N)
{
// Calculate the PI value using the random Projection Method
Static randomnumber dart;
Long K = 0;
For (long I = 1; I <= N; ++ I)
{
Double X = dart. frandom ();
Double Y = dart. frandom ();
// Within the circle
If (x * x + y * Y) <= 1)
++ K;
}
Return 4 * k/double (N );
}
 
Int main ()
{
// When 1,000 times are invested
Cout <darts (1000) <Endl;
// When 10,000 times are invested
Cout <Darts (10000) <endl;
// When 100,000 times are invested
Cout <Darts (100000) <endl;
// When 1,000,000 times are invested
Cout <Darts (1000000) <endl;
// When 10,000,000 times are invested
Cout <Darts (10000000) <endl;
// When 100,000,000 times are invested
Cout <Darts (100000000) <endl;
Return 0;
 
}

 

The Code shows that the more random projection points, the closer it is To 3.1415926...

This is similar to the probability of finding the front and back sides of a coin when throwing a coin. The more lab times, the closer it is to the theoretical value.

The next chapter is the randomization algorithm (3)-Sherwood Algorithm

Tanky Woo is original. You are welcome to repost it. Please attach a link for reprinting. Please do not delete any links to this blog in this article without permission.

Tanky Woo Tag: Numerical random algorithm, random projection method, Pi value


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.