Today I had a sudden whim to try that if I could write a blog with English. I chose a relatively simple question because I'm afraid I cannot describe it in detail.
This topic is about the random number algorithm. We all clearly know that the obvious characteristic of this kind of algorithm is randomness. In general, the more times the random algorithm runs, the more likely the answers will be correct. Compared with deterministic algorithms, the random algorithms generally have simpler codes and lower time complexity. But computers cannot produce really random numbers. In that case, we must find a method to produce a pseudo-random number. Actually the most common one to produce pseudo-random numbers is the linear congruence method. The random sequence "a0, a1, a2..." produced by this method satisfies this expression:
How to select the constants b, c and m in this method is directly related to the random performance of the generated random sequence. M should be sufficiently large, so m can be taken as the machine large number, in addition, the largest common factor gcd(m,b) must be 1, so b can be taken as a prime number. So we can set up a random number class, this class can compute random numbers from 0 to n-1 or numbers from 0 to 1.
Problem Description: A circle of radius r and its tangent square are set. We throw n points at random at the square. Set the number of points that fall into the circle to be k. Because the points you put in are evenly distributed on the square, the probability of the points you put in which fall into the circle is PI*r^2/4*r^2, that is PI/4. In this case, when n is large enough, the ratio of k to n approaches that probability. So PI is roughly equal to 4*k/n.
Here is the source code to define the RandomNumber class and solve the problem. The code comments are also written in English.
#include <time.h> #include <iostream> using namespace std; const unsigned long maxshort = 65536L; //m const unsigned long multiplier = 1194211693L; //b const unsigned long addr = 12345L; //c class RandomNumber{ private: unsigned long randSeed; //randSeed chosen by users or machine public: RandomNumber(unsigned long s = 0){ //0 represents machine choose that if(s == 0) randSeed = time(0); //choose randSeed according to time else randSeed = s; } //output a random number from 0 to n minus 1 unsigned short Random(unsigned long n); //output a random number from 0 to 1 double fRandom(); }; unsigned short RandomNumber :: Random(unsigned long n){ n = 65536; randSeed = multiplier*randSeed+addr; //take 16 bits higher than common rS, %n limit number range return(unsigned short)((randSeed>>16)%n); } double RandomNumber :: fRandom(){ return Random(maxshort)/double(maxshort); } double PAI(int n){ RandomNumber point; int k = 0; for(int i = 1; i <= n; i++){ //x and y of the randomly points double x = point.fRandom(); double y = point.fRandom(); if(x*x+y*y <= 1) k++; } return 4*k/double(n); } int main(){ int n; cout <<"please input a large number:"; cin >>n; cout <<"π is about"<<PAI(n)<<endl; return 0; }It is obvious that when n is 9999999, we got an approxmation of PI.
In conclusion, numerical stochastic algorithm can find an approxmate solution, which is suitable for finding a specific value. Well, here I think this blog is a success. So if you want to practise your English, you can also write your blogs in this same way. After all, you cannot learn to program quickly without English.