C++:利用随机函数,根据高尔顿原理产生正态分布。(打开Excel,利用图表现的向导look)

#include <iostream>
#include <time.h>
#include <fstream>

using namespace std;
//Golton板仿真,将随机数加工为正态分布

int Golton(int theMidPlace,int theLevel)
{
 if ((rand()/10%2)==1)
 {
  if (rand()%2==1)
  {
   theMidPlace++;
  }
  else
   theMidPlace--;
 }
 for (int i = 0;i<theLevel;i++)
 {
  
  if (rand()/4%2==1)
  {
   theMidPlace++;
  }
  else
   theMidPlace--;
 }
 return theMidPlace;
}

//主函数实现正态分布,并把数据存储在Excel

void main()
{
 long x,midplace,level,count[101];
 time_t t;
 srand((unsigned)time(&t));

 ofstream fp;
 fp.open("d://Golton.xls");
    //把数据存储在生成的Excel表格里
 cout<<"输入初值的位置";
 cin>>midplace;
 cout<<endl<<"输入Golten的层数";
 cin>>level;
    for(int j = 0;j<101;j++)
 {
  count[j]=0;
 }
 for(long k =0;k<1000000;k++)
 {
  x=Golton(midplace,level);
  if(x>=0&&x<=100)
  count[x]++;
  else continue;
 }
 for(int i = 0;i<101;i++)
 {
 
  fp<<i<<"/t";
 }
 fp<<endl;
 for(int m = 0; m<101;m++)//把数据读入Excel表格
 {

  
  fp<<count[m]<<"/t";
  
 }
 fp.close();//关闭文件
 
  
 
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值