背景:在一个很大的方形透明容器内,从某个位置放入适量的蚊子,等待几分钟后观察蚊子的分布.
#include
#include
#include
#define random(a,b) (rand() % (b-a+1))+ a
using namespace std;
int main()
{
srand((int)time(0));
int x;
int a[20001] = {0}; //哈希数组,用于记录蚊子的位置
int temp;
for(int i = 0;i < 1000;++i) { //循环1000次代表放进1000只蚊子
x = 0; //x坐标为0,代表蚊子从容器中间放入
for(int j = 0;j < 20000;++j) { //循环20000次代表蚊子运动20000次
temp = random(0,200) - 100; //随机蚊子运动一次的距离,在-100到100之间
x += temp;
while(x < -10000 || x > 10000) { //若蚊子运动超出边界,则重新随机,直到蚊子的位置不超出边界
x -= temp;
temp = random(0,200) - 100;
x += temp;
}
}
a[x+10000]++; //用哈希标记蚊子运动之后的x坐标
}
int b[80] = {0}; //以下是把蚊子的位置按照每250一个区间分80个区间统计输出
for(int i = 0;i < 80;++i) {
for(int j = i * 250;j < (i+1) * 250;++j) {
if(a[j] != 0) {
b[i]++;
}
}
cout << i + 1 << " " << b[i] << endl;
}
/*for(int i = 0;i <= 20000;++i) {
cout << i + 1 << " " << a[i] << endl;
}*/
return 0;
}
当蚊子移动20000次的时候,得到的图像是不符合正态分布的:
当蚊子移动2000次的时候,得到的图像符合正态分布:
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Coming of Age in Second Life
Tom Boellstorff / Princeton University Press / 2008-04-21 / USD 29.95
The gap between the virtual and the physical, and its effect on the ideas of personhood and relationships, is the most interesting aspect of Boellstorff's analysis... Boellstorff's portrayal of a virt......一起来看看 《Coming of Age in Second Life》 这本书的介绍吧!