[gcc &c语言] 随机生成8位的数字字母组合

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[gcc &c语言] 随机生成8位的数字字母组合相关的知识,希望对你有一定的参考价值。

用gcc(我现在linux,gcc最好啦)或c语言编写一段程序代码

用于随机生成一个(多个更好啦)8位(或自定义位数)的数字与字母组合,并保存文件输出

初学致用,还望顺便解释一下各语句的含义。

前辈们请帮忙看看,分少了点,不诚敬意
---本人长期在线---

复制粘贴的死远点,前辈4楼以上的不用看了,你们直接答就是

4楼的前辈,编译我也通过了,但……

hp@hp-desktop:~$ gedit password.c
hp@hp-desktop:~$ gcc password.c -o password
hp@hp-desktop:~$ ./password
段错误 (core dumped)
hp@hp-desktop:~$

en,对,的确是要参数;能否输出多个?能否用汉字?做个错误处理 ?我实在是太菜了……

我还有几个问题,等会一块儿再开问题问了吧……

程序有一个参数,表示生成的密码的长度
运行的时候要加上,比如./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; /*程序结束*/
参考技术A 有专门教这个的网站,自己百度下了 参考技术B 我记得好象是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 ValidateCode(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();


在excel中怎样生成随机的4位字母数字组合,谢谢

产生大写字母与数字的组合
=CHAR(RANDBETWEEN(65,90))
=IF(RAND()<0.5,CHAR(RANDBETWEEN(48,57)),CHAR(RANDBETWEEN(65,90))) &IF(RAND()<0.5,CHAR(RANDBETWEEN(48,57)),CHAR(RANDBETWEEN(65,90)))&IF(RAND()<0.5,CHAR(RANDBETWEEN(48,57)),CHAR(RANDBETWEEN(65,90)))&IF(RAND()<0.5,CHAR(RANDBETWEEN(48,57)),CHAR(RANDBETWEEN(65,90)))
参考技术A =if(randbetween(0,1),randbetween(0,9),char(randbetween(65,90)))&if(randbetween(0,1),randbetween(0,9),char(randbetween(65,90)))&if(randbetween(0,1),randbetween(0,9),char(randbetween(65,90)))&if(randbetween(0,1),randbetween(0,9),char(randbetween(65,90)))

以上是关于[gcc &c语言] 随机生成8位的数字字母组合的主要内容,如果未能解决你的问题,请参考以下文章

excel怎样根据给定的字符生成一个8位的随机字符

怎么用c语言随机生成一串字符串,字符串要求8-16位并且包含大小写字母.数字.和符号

excel 如何自动生成32位的大写字母跟数字组合的随机序列,类似于验证码的那种随机的

c语言 随机生成一个32位的16进制数的代码

在excel中怎样生成随机的4位字母数字组合,谢谢

mkpasswd命令 随机生成密码