萌新做点小玩意儿DAY-10 随机数算法计算π的值

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.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值