程序有一个参数,表示生成的密码的长度运行的时候要加上,比如./password 8我写的很简单,参数没做检查,你应该自己去完善一下。
#include <stdlib.h>#include <string.h>#include <stdio.h>void generate(int len,char* buffer){/*产生密码用的字符串*/static const char string[]= "0123456789abcdefghiljklnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";int i = 0;for(; i < len; i++){buffer[i] = string[rand()%strlen(string)]; /*产生随机数*/}}int main(int argc, char* argv[]){int len = atoi(argv[1]); /*指定生成的密码长度*/srand(time(0)); /*设定随机数种子*/char *buffer = (char*)malloc(len + 1); /*分配内存*/generate(len,buffer); /*生成密码*/puts(buffer); /*输出到屏幕*/FILE* fp = fopen("pass","w"); /*打开输出文件*/if(fp == NULL)return -1;fwrite(buffer, sizeof(char), len, fp); /*写文件*/fclose(fp); /*关闭文件*/free(buffer); /*释放动态分配的内存*/return 0; /*程序结束*/}自己可以写一个函数。
IT生活系列群:50337593 IT-live(软件开发)计算机软件技术群,技术是相通的,大家取长补短,共同进步吧!--软件不仅仅是一种思想。
他是一门艺术30633141 IT-live(网站建设)网虫们的天地。
大家要努力把我们的家园建得更好。
Asp、Php、Jsp、50337724 IT-live(心理就诊)工作生活中难免会有不开心、不痛快地事我们有专业的心理医师有热心的IT朋友们,让大家帮助你解除困惑。
让你开心每一天!50337649 IT-live(项目外包)证明你的实力,实现你的目标。
80%的能力+100%的努力=成功!!!!!!!3431641 IT-live(招聘求职)面包很重要。
有了它,才能走遍天下!1949892 it_live(美工设计)再好的软件。
没有好的界面是不行的。
欢迎IT爱好者的加入。
愿我们在这共同的群体中,找到自己想要的生活!!!!我记得好象是rand函数吧,可以生成随机数,至于具体的位数,是有参数选择的这是完整的你只要进去改改数字就可以生成8位的了:希望对你有用!using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Drawing.Imaging;using System.Drawing;using System.IO;public partial class UserControls_Gif : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){string tmp = RndNum(5).ToUpper();this.Session["ImageV"] = tmp;HttpCookie a = new HttpCookie("ImageV", tmp);Response.Cookies.Add(a);this.CreateCheckCodeImage(tmp);}private void V alidateCode(string VNum){Bitmap Img = null;Graphics g = null;MemoryStream ms = null;int gheight = VNum.Length * 15;Img = new Bitmap(gheight, 30);g = Graphics.FromImage(Img);//背景颜色g.Clear(Color.White);//文字字体Font f = new Font("Arial Black", 11);//文字颜色SolidBrush s = new SolidBrush(Color.Black);g.DrawString(VNum, f, s, 3, 3);ms = new MemoryStream();Img.Save(ms, ImageFormat.Jpeg);Response.ClearContent();Response.ContentType = "image/Jpeg";Response.BinaryWrite(ms.ToArray());g.Dispose();Img.Dispose();Response.End();}private string RndNum(int VcodeNum){string Vchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p" + ",q,r,s,t,u,v,w,x,y,z";string[] VcArray = Vchar.Split(new Char[] { ',' });string VNum = "";int temp = -1;Random rand = new Random();for (int i = 1; i < VcodeNum + 1; i++){if (temp != -1){rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));}int t = rand.Next(35);if (temp != -1 && temp == t){return RndNum(VcodeNum);}temp = t;VNum += VcArray[t];}return VNum;}private void CreateCheckCodeImage(string checkCode){if(checkCode == null || checkCode.Trim() == String.Empty)return;System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 15.5)), 30);Graphics g = Graphics.FromImage(image);try{//生成随机生成器Random random = new Random();//清空图片背景色g.Clear(Color.White);//画图片的背景噪音线for(int i=0; i<25; i++){int x1 = random.Next(image.Width);int x2 = random.Next(image.Width);int y1 = random.Next(image.Height);int y2 = random.Next(image.Height);g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);}Font font = new System.Drawing.Font("Arial", 14, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);g.DrawString(checkCode, font, brush, 2, 2);//画图片的前景噪音点for(int i=0; i<100; i++){int x = random.Next(image.Width);int y = random.Next(image.Height);image.SetPixel(x, y, Color.FromArgb(random.Next()));}//画图片的边框线g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);System.IO.MemoryStream ms = new System.IO.MemoryStream();image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);Response.ClearContent();Response.ContentType = "image/Gif";Response.BinaryWrite(ms.ToArray());}finally{g.Dispose();image.Dispose();}}}#include<stdio.h>#include<stdlib.h>#include<time.h>main(){srand(time(0));int a=rand()%900000+100000;printf("随机数%d",a);}#include<stdio.h>#include<stdlib.h>#include<time.h>int main(){srand( time(NULL) );int a= rand()%900000 + 100000*(rand()%9+1);printf("随机数%d\n",a);return 0;}C语言下没有真正的随机函数,如果用srand与rand配合产生随机数,每个数之间的间隔需要在1秒钟以上. 下面是我写的一个用来随机产生7-9位QQ邮箱的源代码,原理是先用随机函数产生一个<10的整数(这个数是所生成QQ号码的位数),然后判断这个数是否在7~9之间,如果满足要求,下面将一位一位地产生每一位数字,最后与"@"连接,生成一个QQ邮箱,并写入文件.VC++6.0下编译通过函数produceQQMail()用来产生随机数的代码:#include<windows.h>#include<stdio.h>#include<stdlib.h>#include<time.h>#define MAX_ADDRESS 1000char directory[20];DWORD writeFile(const char*filename,char*str){FILE *fp;fp=fopen(filename,"a+");if(fp==NULL){puts("Cannot open this file!");return -1;}fprintf(fp,"%s\n",str);fflush(fp);//fputs(str,fp);return 1;}DWORD produceQQMail() //→→→→→产生QQ邮箱函数{unsigned int i,count;char zj[20];char compare[20];unsigned long relative;for(count=1;count<=MAX_ADDRESS;count++){Sleep(600);srand((unsigned)time(0));while(1){relative=1+(int)(10.0*rand()/(RAND_MAX+1.0));//产生QQ号码的位数if(relative<7||relative>9)continue;//判断是否满足所需要的位数Sleep(100);for(i=0;i<relative;i++){itoa(1+(int)(10.0*rand()/(RAND_MAX+1.0)),&zj[i],10);//生成每一位数字,将其转换为字符型,并保存在数组中}zj[i]='\0';strcat(zj,"@");//连接生成邮箱if(count>1&&strcmp(compare,zj)==0) //比较前后两者以防出现相同的邮箱continue;strcpy(compare,zj);if(writeFile(directory,zj)==1){printf("===>生成第%d个QQ邮箱\n",count);//system("cls");break;}}}return 1;}void main(){puts("=>请输入您要保存到的文件名及其目录");scanf("%s",directory);puts("=>Now producing QQ mail addresses……");produceQQMail();}随机数是怎么产生出来的呢?可以利用C语言中的种子函数srand( )和伪随机函数rand( )来实现。