函数代码
#include <string>bool GenerateRandom(std::string *str,unsigned int len)
{srand(time(NULL));for(unsigned int i=0;i<len;i++){switch(rand()%3){case 1:(*str).push_back('A'+rand()%26);break;case 2:(*str).push_back('a'+rand()%26);break;default:(*str).push_back('0'+rand()%10);break;}}return true;
}
主函数调用
- 生成指定位数的随机数,例子是显示输出32位的随机数
#include <iostream>
#include "include/random.h"int main(void)
{std::string str;GenerateRandom(&str,32);cout << "GenerateRandom:" << str << "\n";}