#include <iostream>
#include <ctime>
using namespace std;
string randstr(string strCharElem,int nOutStrLen)
{
string strRet;
strRet.insert(0,nOutStrLen,'\0');
srand((unsigned)time(0));
int iRand = 0;
for (int i = 0; i < nOutStrLen; ++i)
{
iRand = rand() % strCharElem.length();
strRet[i] = strCharElem[iRand];
}
return strRet;
}
int main()
{
string str = randstr("abcdefghigklmnopqrstuvwxyz",5);
cout << "randstr Result:" << endl << str.c_str() << endl;
getchar();
return 0;
}
运行结果: