产生随机字符数组的一种方法

本文展示了一个使用C++生成随机字符数组的示例代码。该程序利用了srand和rand函数来确保每次运行时都能生成不同的随机字符组合,并通过循环输出了多个由数字和小写字母构成的随机字符数组。

代码贴上:

#include <stdlib.h>
#include
<string.h>
#include
<time.h>
#include
<iostream>

// using namespace std;

int main()
{
const char *c = "0123456789abcdefghijklmnopqrstuvwxyz";

srand(static_cast
<unsigned int>(time(NULL)));
std::cout
<< "The random char array is:";
for(int i = 0; i < 5; ++i)
{
std::cout
<< std::endl;
int index = 0 ;
for(int j = 0; j < 5; ++j)
{
index
= rand() % strlen(c);
std::cout
<< c[index ]
<< c[index]
<< " ";
}
}
std::cout
<< std::endl;
return 0;
}

运行效果如下:

The random char array is:
00 qq 88 ii zz
qq bb oo 00 yy
rr yy tt ww ll
ss rr aa bb oo
11 yy tt 33 uu
请按任意键继续. . .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值