C语言生成随机字符串

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Knowledgebeggar/article/details/79967110
// C语言生成随机字符.
// VS2017 15.6.4

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

const unsigned char allChar[63] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
void generateString(unsigned char * dest, unsigned int len);

int main()
{
	const unsigned int strlen = 20;
	unsigned char str[strlen + 1] = {};

	generateString(str, strlen);

	printf("%s\n", str);
	
    return 0;
}

void generateString(unsigned char * dest, const unsigned int len)
{
	unsigned int cnt, randNo;
	srand((unsigned int)time(NULL));

	for (cnt = 0; cnt<len; cnt++)
	{
		randNo = rand() % 62;
		*dest = allChar[randNo];
		dest++;
	}

	*dest = '\0';
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页