主题:为何我的程序两次运行结果不同?
我编了一程序,1)把一个数组species[7][9]中的值展开形成一个新的数组new_array;2)从新组new_array中随机取几个值再组成一个数组rarefaction_array,并计算这个数组中元素个数;3)当重复多次后,计划元素的平均个数TOTAL_REPEAT(5000).
在数理统计上,当重复次数特别大时,这个值应该是一个定值.(此过程即为bootstrap过程,也是rarefaction的核心思想).但是为何我两次运行结果不一样呢?
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
// a,b,c,d,e,f,g,h,i
int species[7][9]={{151,15,1,0,0,0,0,0,0},//B.rufofasicatus
{78,1,1,0,4,0,0,0,0},//B.lucorum
{44,2,0,0,0,0,0,0,0},//B.ladakhensis
{6,10,0,0,0,0,1,1,1},//B.lepidus
{1,13,0,1,0,0,0,0,0},//B.impetuosus
{3,3,0,4,0,2,1,0,0},//B.kashmirensis
{6,4,0,0,0,1,0,0,0}};//B.friseanus
#define COLUMN 7
#define ROW 9
#define MINNUMBER 11
#define TOTAL_REPEAT 5000
// above define the database
int get_array_number (char *rarefaction_array);
void calculate_array (char *new_array, int arraylength,int repeattimes);
// above is to define those function
void main ( )
{
// below is the main party of the programme
for (int i=0;i<COLUMN;i++)
{
int arraylength=0;
for (int j=0;j<ROW;j++)//sum the total individual in each column
{
arraylength+=species[i][j];
}
char *new_array=new char[arraylength];// Build a array with sum individul
for (int x=0;x<arraylength;x++)//give each member of the array 0
{
new_array[j]=0;
}
char arraystuff='a';//Begin arraystuff number
arraylength=0;//redefine the number
for (int a=0;a<ROW;a++)// construct the new_array
{
if (species[i][a]!=0)
{
for (int z=0;z<species[i][a];z++)
{
new_array[arraylength]=arraystuff;
arraylength++;
}
arraystuff++;
}
}// the author construct the new_array with number of row.
calculate_array (new_array,arraylength,i);//this party will send the new_array to
//calculate and print the result, respecitively
delete new_array;//release the memorry
}
}
//above is the main part of the programme
void calculate_array(char *new_arrow,int arraylength,int repeattimes)
{
unsigned long sum=0;
int repeat=0;
char rarefaction_array[MINNUMBER];//define the rarefaction_array
while (repeat<=TOTAL_REPEAT) //do totoal_repeat circle to calculate the sum
{
srand((unsigned)time(NULL));
for (int i=0; i<MINNUMBER; i++)
{
int random_number;
random_number=rand()%arraylength;
rarefaction_array[i]=new_arrow[random_number];
};//Constract the rarefaction_array
sum=sum+get_array_number(rarefaction_array);//get the number in the rarefaction_array;
repeat++; //control while sentence
}
cout<<" the food plant chooce of "<<repeattimes+1<<" species is : "<< (float)sum/TOTAL_REPEAT <<endl; // print the result
}
//Above is to rarefact the array
//Below is to get the number of idividual in the rarefaction array
int get_array_number (char *rarefaction_array)
{
{
char *temp=new char[MINNUMBER];
int count=0;
for (int j=0;j<MINNUMBER;j++)
temp[j]=0; //give each member of the array a ZERO
bool flag;
for (int i=0;i<MINNUMBER;i++)
{
flag=true;
for (int z=0;z<count;z++)
{
if (temp[z]==rarefaction_array[i])
{
flag=false;
break;
}
}
if (flag==true)
temp[count++]=rarefaction_array[i];
}
delete temp;
return count;
}// to control the calculation faction
}
//above is to get the number of idividual in the rarefaction array
在数理统计上,当重复次数特别大时,这个值应该是一个定值.(此过程即为bootstrap过程,也是rarefaction的核心思想).但是为何我两次运行结果不一样呢?
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
// a,b,c,d,e,f,g,h,i
int species[7][9]={{151,15,1,0,0,0,0,0,0},//B.rufofasicatus
{78,1,1,0,4,0,0,0,0},//B.lucorum
{44,2,0,0,0,0,0,0,0},//B.ladakhensis
{6,10,0,0,0,0,1,1,1},//B.lepidus
{1,13,0,1,0,0,0,0,0},//B.impetuosus
{3,3,0,4,0,2,1,0,0},//B.kashmirensis
{6,4,0,0,0,1,0,0,0}};//B.friseanus
#define COLUMN 7
#define ROW 9
#define MINNUMBER 11
#define TOTAL_REPEAT 5000
// above define the database
int get_array_number (char *rarefaction_array);
void calculate_array (char *new_array, int arraylength,int repeattimes);
// above is to define those function
void main ( )
{
// below is the main party of the programme
for (int i=0;i<COLUMN;i++)
{
int arraylength=0;
for (int j=0;j<ROW;j++)//sum the total individual in each column
{
arraylength+=species[i][j];
}
char *new_array=new char[arraylength];// Build a array with sum individul
for (int x=0;x<arraylength;x++)//give each member of the array 0
{
new_array[j]=0;
}
char arraystuff='a';//Begin arraystuff number
arraylength=0;//redefine the number
for (int a=0;a<ROW;a++)// construct the new_array
{
if (species[i][a]!=0)
{
for (int z=0;z<species[i][a];z++)
{
new_array[arraylength]=arraystuff;
arraylength++;
}
arraystuff++;
}
}// the author construct the new_array with number of row.
calculate_array (new_array,arraylength,i);//this party will send the new_array to
//calculate and print the result, respecitively
delete new_array;//release the memorry
}
}
//above is the main part of the programme
void calculate_array(char *new_arrow,int arraylength,int repeattimes)
{
unsigned long sum=0;
int repeat=0;
char rarefaction_array[MINNUMBER];//define the rarefaction_array
while (repeat<=TOTAL_REPEAT) //do totoal_repeat circle to calculate the sum
{
srand((unsigned)time(NULL));
for (int i=0; i<MINNUMBER; i++)
{
int random_number;
random_number=rand()%arraylength;
rarefaction_array[i]=new_arrow[random_number];
};//Constract the rarefaction_array
sum=sum+get_array_number(rarefaction_array);//get the number in the rarefaction_array;
repeat++; //control while sentence
}
cout<<" the food plant chooce of "<<repeattimes+1<<" species is : "<< (float)sum/TOTAL_REPEAT <<endl; // print the result
}
//Above is to rarefact the array
//Below is to get the number of idividual in the rarefaction array
int get_array_number (char *rarefaction_array)
{
{
char *temp=new char[MINNUMBER];
int count=0;
for (int j=0;j<MINNUMBER;j++)
temp[j]=0; //give each member of the array a ZERO
bool flag;
for (int i=0;i<MINNUMBER;i++)
{
flag=true;
for (int z=0;z<count;z++)
{
if (temp[z]==rarefaction_array[i])
{
flag=false;
break;
}
}
if (flag==true)
temp[count++]=rarefaction_array[i];
}
delete temp;
return count;
}// to control the calculation faction
}
//above is to get the number of idividual in the rarefaction array