实验对象随机分组程序
实验室小鼠随机方法
老师上统计课的时候讲了实验室对实验小鼠进行随机分组的方法。
方法有点麻烦,如下:
0. n为总数,m为组数,o为每个小组的最大数量,o=n/m
1. 对每个小鼠进行编号
2. 依次对每个小鼠取随机数,并把这个随机数除以组数求余,余数为初次分配的组数
3. 对多于数量o的小组,取随机数求余的方式随机确定一个成员,放到第一个未满的小组
4. 重复执行第3步,直到所有组都为o个小鼠,分组完成
代码实现
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define OVERFLOW -1
typedef struct
{
int count;
int * list;
} Group;
void InitInput(int * totalAmount,int * groupAmount) //最开始的输入环节
{
int flag;
do
{
flag=0;
printf("请输入样本总数: ");
scanf("%d",totalAmount);
printf("请输入组数: ");
scanf("%d",groupAmount);
if(*totalAmount<=0)
{
printf("total Amount not avaliable,please input again\n");
flag=1;
}
else if(*groupAmount<=0)
{
printf("group Amount not avaliable,please input again\n");
flag=1;
}
else if(*totalAmount%*groupAmount!=0)
{
printf("不能整除");
flag=1;
}
else
{
flag=0;
}
printf("\n");
}
while(flag==1);
printf("样本总数为%5d,组数为%5d,每组有%5d个样本\n",*totalAmount,*groupAmount,*totalAmount/(*groupAmount));
}
void PrintGroups(Group * groups)
{
int i,j;
int groupAmount=groups[0].count;
for(i=1; i<=groupAmount; i++)
{
printf("\n第%2d组:\t",i);
for(j=0; j<groups[i].count; j++)
{
printf("%6d",groups[i].list[j]);
if(j%10==9 && j!=groups[i].count-1) printf("\n\t");
}
}
printf("\n");
}
void InitWorkGroups(Group * * groups,int totalAmount,int groupAmount)
{
int i;
(*groups)=(Group*)malloc(groupAmount*sizeof(Group));
if(!*groups) exit(OVERFLOW);
(*groups)[0].count=groupAmount;
for(i=1; i<=groupAmount; i++)
{
(*groups)[i].count=0;
if(!((*groups)[i].list=(int*)malloc(totalAmount*sizeof(int))) ) exit(OVERFLOW);
}
printf("InitWorkGroups Complete\n");
}
void DestroyWorkGroups(Group **groups){
int i;
for(i=1;i<=(*groups)[0].count;i++){
free((*groups)[i].list);
}
free(*groups);
*groups=NULL;
}
void FirstAlloc(Group * groups,int totalAmount) //第一轮分配
{
int groupAmount=groups[0].count;
int rnd,rndMod;
int i;
printf("\n开始第一轮分配\n");
for(i=1; i<=totalAmount; i++)
{
rnd=rand();
rndMod=rnd%groupAmount;
rndMod=rndMod==0?groupAmount:rndMod;
groups[rndMod].list[groups[rndMod].count++]=i;
printf("%d:%5d->%2d \t",i,rnd,rndMod);
if(i%5==0) printf("\n");
}
}
void NextAlloc(Group * groups,int groupSize) //第二轮分配
{
int groupAmount=groups[0].count;
int i,j;
int rnd,rndMod;
int num;
printf("\n开始第二轮分配\n");
//groupSize=totalAmount/groupAmount;
for(i=1; i<=groupAmount; i++)
{
while(groups[i].count>groupSize)
{
rnd=rand();
rndMod=rnd%groups[i].count;
rndMod=rndMod==0?groups[i].count:rndMod;
num=groups[i].list[rndMod-1];
for(j=rndMod-1; j<groups[i].count-1; j++)
{
groups[i].list[j]=groups[i].list[j+1];
}
groups[i].count--;
for(j=1; j<=groupAmount; j++)
{
if(groups[j].count<groupSize)
{
groups[j].list[groups[j].count++]=num;
break;
}
}
printf("组:%2d,随机数:%5d,求余:%2d,编号:%d-->group:%2d\n",i,rnd,rndMod,num,j);
}
}
}
int main(int argc,char ** argv)
{
int totalAmount,groupAmount,groupSize;
Group * groups;
srand(time(0));
InitInput(&totalAmount,&groupAmount);
groupSize=totalAmount/groupAmount;
InitWorkGroups(&groups,totalAmount,groupAmount);
FirstAlloc(groups,totalAmount);
PrintGroups(groups);
system("pause");
NextAlloc(groups,groupSize);
PrintGroups(groups);
system("pause");
DestroyWorkGroups(&groups);
return 0;
}