题目:

这个程序的原理是这样的。假如有一个边长为1的正方形。以正方形的一个端点为圆心,以1为半径,画一个圆弧,于是在正方形内就有了一个直角扇形。在正方形里随机生成若干的点,则有些点是在扇形内,有些点是在扇形外。正方形的面积是1,扇形的面积是0.25*Pi。设点的数量一共是n,扇形内的点数量是nc,在点足够多足够密集的情况下,会近似有nc/n的比值约等于扇形面积与正方形面积的比值,也就是nc/n= 0.25*Pi/1,即Pi = 4*nc/n。

实现思路:

通过map读入文件,文件内容为投掷次数,暂时设定为100次,共10次。

然后map中,生成随机数,即x y点的坐标,计算点到(0,0)的距离,如果小于1加入到计数器in中,大于1则加入计数器out中,然后计算出pi值

reduce中,将求得的pi值再次进行求平均值。

代码如下

package Demo3;

/**
* @author 星际毁灭
* 使用算法随机生成xy的坐标
* */
public class Pi {
static int digit = 40;
private int[] bases= new int[2];
private double[] baseDigit = new double[2];
private double[][] background = new double[2][digit];
private long index; Pi(int[] base) {
bases = base.clone();
index = 0; for(int i=0; i<bases.length; i++) {
double b = 1.0/bases[i];
baseDigit[i] = b;
for(int j=0; j<digit; j++) {
background[i][j] = j == 0 ? b : background[i][j-1]*b;
}
}
} double[] getNext() {
index++; double[] result = {0,0}; for(int i=0; i<bases.length; i++) {
long num = index;
int j = 0;
while(num != 0) {
result[i] += num % bases[i] * background[i][j++];
num /= bases[i];
}
} return result;
} public static void main(String[] args) {
int[] base = {2,5};
Pi test = new Pi(base);
for(int x = 0; x < 100; x++){
double[] t = test.getNext();
System.out.println(t[0] + "\t" + t[1]);
} } }
package Demo3;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Reducer.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import Demo1.WordCountTest;
/**
* @author 星际毁灭
* 求pi的值
*
* */
public class GetPoint {
public static class Map extends Mapper<Object , Text , Text , Text>{
private static Text newKey=new Text();
private static final IntWritable one = new IntWritable(1);
public void map(Object key,Text value,Mapper<Object, Text, Text, Text>.Context context) throws IOException, InterruptedException{
String line=value.toString();
int num=Integer.parseInt(line); //读取生成的点的数量
int[] base = {2,5}; //生成pi的xy坐标
Pi test = new Pi(base); //生成pi的xy坐标
int in=0; //在圆内
int out=0; //在圆外
newKey.set("pi");
System.out.println(num);
for(int x = 0; x < num; x++){
double[] t = test.getNext();//生成pi的xy坐标
//System.out.println(t[0] + "\t" + t[1]);
if(t[0]*t[0]+t[1]*t[1]<=1) { //该点到原点的距离小于等于1
in++;
}else {
out++;
}
}
double pi=4.0000000000*in/num; //求pi的值
context.write(newKey,new Text(pi+"")); //输出结果
}
}
public static class Reduce extends Reducer<Text, Text, Text, Text>{
public void reduce(Text key,Iterable<Text> values,Context context) throws IOException, InterruptedException{
double sum=0;
int num=0;
for(Text val:values){ //求均值
sum+=Double.parseDouble(val.toString());
num++;
//context.write(key,val);
}
double pi=sum/num; //求pi的值
String p=""+pi;
context.write(key,new Text(p)); //输出结果
}
} public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
System.setProperty("hadoop.home.dir", "H:\\文件\\hadoop\\hadoop-2.6.4");
Configuration conf=new Configuration();
Path in=new Path("hdfs://192.168.6.132:9000/wys/in/pi.txt");
Path out=new Path("hdfs://192.168.6.132:9000/wys/out/piout");
// FileInputFormat.setMaxInputSplitSize(job, size);
Job job =new Job(conf,"OneSort");
FileInputFormat.addInputPath(job,in);
FileOutputFormat.setOutputPath(job,out); job.setJarByClass(GetPoint.class);
job.setMapperClass(GetPoint.Map.class);
job.setReducerClass(GetPoint.Reduce.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.waitForCompletion(true); System.exit(job.waitForCompletion(true) ? 0 : 1);
} }

Mapreduce案例之Pi值估算的更多相关文章

  1. mapreduce案例:获取PI的值

    mapreduce案例:获取PI的值 * content:核心思想是向以(0,0),(0,1),(1,0),(1,1)为顶点的正方形中投掷随机点. * 统计(0.5,0.5)为圆心的单位圆中落点占总落 ...

  2. Hadoop下MapReduce实现Pi值的计算

    Hadoop自带的例子中,有一个计算Pi值的例子. 这个程序的原理是这样的.假如有一个边长为1的正方形.以正方形的一个端点为圆心,以1为半径,画一个圆弧,于是在正方形内就有了一个直角扇形.在正方形里随 ...

  3. 【Hadoop离线基础总结】MapReduce案例之自定义groupingComparator

    MapReduce案例之自定义groupingComparator 求取Top 1的数据 需求 求出每一个订单中成交金额最大的一笔交易 订单id 商品id 成交金额 Order_0000005 Pdt ...

  4. 整理打印PI值

    准备锻炼背诵PI的小数,找到PI值: PI=3. 141592653589793238462643383279502884197169399375105820974944592307816406286 ...

  5. c++中sin,cos,arcsin等和在C/C++中使用pi (π) 值

    先 #include<math.h> 反3角函数有 acos(double),asin(double),atan(double),atan(double,double),返回值 doubl ...

  6. Hadoop Mapreduce 案例 wordcount+统计手机流量使用情况

    mapreduce设计思想 概念:它是一个分布式并行计算的应用框架它提供相应简单的api模型,我们只需按照这些模型规则编写程序,即可实现"分布式并行计算"的功能. 案例一:word ...

  7. 计算圆周率 Pi (π)值, 精确到小数点后 10000 位 只需要 30 多句代码

    大家都知道π=3.1415926……无穷多位, 历史上很多人都在计算这个数, 一直认为是一个非常复杂的问题.现在有了电脑, 这个问题就简单了.电脑可以利用级数计算出很多高精度的值, 有关级数的问题请参 ...

  8. 【尚学堂·Hadoop学习】MapReduce案例2--好友推荐

    案例描述 根据好友列表,推荐好友的好友 数据集 tom hello hadoop cat world hadoop hello hive cat tom hive mr hive hello hive ...

  9. 【尚学堂·Hadoop学习】MapReduce案例1--天气

    案例描述 找出每个月气温最高的2天 数据集 -- :: 34c -- :: 38c -- :: 36c -- :: 32c -- :: 37c -- :: 23c -- :: 41c -- :: 27 ...

随机推荐

  1. Linux上,最常用的一批命令解析(10年精选)

    Linux这么多命令,通常会让初学者望而生畏.下面是我结合日常工作,以及在公司的内部培训中,针对对Linux不是很熟悉的同学,精选的一批必须要搞懂的命令集合.任何一个命令其实都是可以深入的,比如tai ...

  2. 学习笔记:CentOS7学习之二十二: 结构化命令case和for、while循环

    目录 学习笔记:CentOS7学习之二十二: 结构化命令case和for.while循环 22.1 流程控制语句:case 22.2 循环语句 22.1.2 for-do-done 22.3 whil ...

  3. js中遍历对象的属性和值的方法

    鉴于循环目标是个对象,length是为undefined,用map等对数组的循环方法不行,对象就用此下方法 for(var key in _this.lists.medicines){ medicin ...

  4. java-监听器(Listener)

    监听器:用于监听web应用中某些对象.信息的创建.销毁等动作,服务器会自动调用相应的方法进行处理.常用于统计在线人数,初始化系统参数等. Javaweb监听器主要监听对象有ServletContext ...

  5. 【AtCoder】AGC002

    AGC002 A - Range Product #include <bits/stdc++.h> #define fi first #define se second #define p ...

  6. work note

    eclipse git 察看历史 左边是提交的 import { NgModule } from '@angular/core'; import { IonicPageModule } from 'i ...

  7. 网络流+最小生成树的最少割边数--How Many to Be Happy?

    题意:https://blog.csdn.net/Ratina/article/details/95200594 思路: 首先我们知道最小生成树就是按长度枚举边,能连就连. 那么,如果这条边在最小生成 ...

  8. 微信小程序 基本介绍及组件

    创建项目 微信开发工具深入介绍 https://developers.weixin.qq.com/miniprogram/dev/devtools/devtools.html 基本项目目录 1. 配置 ...

  9. xpath的一些常用使用

    xml文档<html> <head> <title>My page</title> </head> <body> <h2& ...

  10. Go语言注意事项

    必须恰当导入需要的包,缺少了必要的包或者导入了不需要的包,程序都无法编译通过.这项严格要求避免了程序开发过程中引入未使用的包(译注:Go语言编译过程没有警告信息,争议特性之一 import 声明必须跟 ...