函數代碼
#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";}
?