码迷,mamicode.com
首页 > 编程语言 > 详细

用C/C++生成随机中文名字

时间:2014-09-10 00:16:49      阅读:442      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   for   div   sp   log   

今天写了个这个功能,有点粗糙,可能会有很多地方要修改,分享下:

  1 #include <stdio.h>
  2 #include <windows.h>
  3 #include <time.h>
  4 
  5 //499个   0-498,百家姓
  6 const char NameHead[500][10] = {"","","","",.....}
  7 //322个,词语
  8 const char Name2[323][10]= {"骄奢","荫庇","芳馨",...}
  9 //52个,大小写字母
 10 const char Name3[53][4] = {"A","B","C","D","E",....}
 11 
 12 //名字长度
 13 #define NAME_LENGTH 6
 14 
 15 //修改随机步长
 16 VOID SetSrand()
 17 {
 18     static ULONG timeindex = 0;
 19     srand((int)time(0) + timeindex);
 20     timeindex += 0x3;
 21     if(timeindex > 0xfffffff)
 22     {
 23         timeindex = 0;
 24     }
 25 }
 26 //获取随机数
 27 int GetRand(int rval)
 28 {
 29     return (int)(rand() % rval);
 30 }
 31 
 32 void getRandName(PCHAR randName)
 33 {
 34     int cnt = 0;
 35     SetSrand();
 36     //srand((unsigned)time(NULL));
 37     int iRange1 = 0xf7 - 0xb0;
 38     int iRange2 = 0xfe - 0xa1;
 39     //40%概率
 40     if(GetRand(100)<=40){
 41         
 42         //printf("1-百家姓+随机字:");
 43         //
 44         cnt = GetRand(sizeof(NameHead)/sizeof(NameHead[0])-1);
 45         lstrcat(randName,NameHead[cnt]);
 46         //
 47         cnt = NAME_LENGTH - (strlen(randName)/2);
 48         //cnt = rand()%(sizeof(CouArry)/sizeof(int));
 49         
 50         for (int i=0;i<cnt;++i)
 51         {
 52             
 53             BYTE iCode1 = GetRand(iRange1) + 0xb0;
 54             
 55             BYTE iCode2 = GetRand(iRange2) + 0xa1;
 56             char ch[3] = {iCode1,iCode2,0};
 57             lstrcat(randName,ch);
 58             
 59         }
 60         
 61     }else if (GetRand(100)<=25)
 62     {
 63         //25%概率
 64         //printf("2-词+随机字:");
 65         
 66         //
 67         cnt = GetRand(sizeof(Name2)/sizeof(Name2[0])-1);
 68         lstrcat(randName,Name2[cnt]);
 69 
 70         //
 71         cnt = NAME_LENGTH - (strlen(randName)/2);
 72 
 73         for (int i=0;i<cnt;++i)
 74         {
 75             //尾巴,1/5概率有
 76             if (GetRand(5)==1)
 77             {
 78                 lstrcat(randName,Name3[GetRand(sizeof(Name3)/sizeof(Name3[0])-1)]);
 79             }else{
 80                 BYTE iCode1 = GetRand(iRange1) + 0xb0;
 81 
 82                 BYTE iCode2 = GetRand(iRange2) + 0xa1;
 83                 char ch[3] = {iCode1,iCode2,0};
 84                 lstrcat(randName,ch);
 85             }
 86             
 87         }
 88         
 89         
 90 
 91     }else{
 92         //printf("3-百家姓+词+随机字+字母:");
 93         //
 94         
 95         cnt = GetRand(sizeof(NameHead)/sizeof(NameHead[0])-1);
 96         lstrcat(randName,NameHead[cnt]);
 97         //
 98         
 99         cnt = GetRand(sizeof(Name2)/sizeof(Name2[0])-1);
100         
101         lstrcat(randName,Name2[cnt]);
102 
103         //cnt = NAME_LENGTH - (strlen(randName)/2);
104 
105         if ((strlen(randName)/2 < NAME_LENGTH))
106         {
107             cnt = NAME_LENGTH - (strlen(randName)/2);
108             for (int i = 0;i<cnt;i++)
109             {
110                 if (GetRand(5)==1)
111                 {
112                     BYTE iCode1 = GetRand(iRange1) + 0xb0;
113 
114                     BYTE iCode2 = GetRand(iRange2) + 0xa1;
115                     char ch[3] = {iCode1,iCode2,0};
116                     lstrcat(randName,ch);
117 
118                 }else{
119                     lstrcat(randName,Name3[GetRand(sizeof(Name3)/sizeof(Name3[0])-1)]);
120                 }
121                 
122             }
123             
124         }
125     }
126     
127 
128     
129 }
130 
131 
132 int main()
133 {
134     
135     char Name[20] = {0};
136     FILE* f = NULL;
137 
138     f = fopen("c:\\log\\name.txt","w+");
139     //100个随机名字
140     for(int i = 0;i<100;i++)
141     {
142         strset(Name,0);
143         getRandName(Name);
144         printf("---------%d:%s\r\n",(i+1),Name);
145         
146         fprintf(f,"%s\r\n",Name);
147         Sleep(10);
148         
149 
150     }
151     
152     
153     getchar();
154 }

写的还不够智能,希望以后能多多补充,多多提炼下自己

用C/C++生成随机中文名字

标签:style   blog   color   io   ar   for   div   sp   log   

原文地址:http://www.cnblogs.com/kindom/p/3963610.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!