|
发表于 2013-6-6 12:45:43
|
显示全部楼层
改进下运算速度- area<-function(radius=1,MonteCarlo_point_num=1000){
- randpoint_x=runif(MonteCarlo_point_num,0,2*radius)
- randpoint_y=runif(MonteCarlo_point_num,0,2*radius)
- distance_randpoint_circlecenter<-sapply(1:MonteCarlo_point_num,function(x) sqrt((randpoint_x[x]-radius)^2+(randpoint_y[x]-radius)^2))
- point_num_in_circle<-length(distance_randpoint_circlecenter[distance_randpoint_circlecenter<radius])
- percent=point_num_in_circle/MonteCarlo_point_num
- area=percent*(2*radius)^2
- return(area)
- }
- area(1,100000)
复制代码 |
|