How to get random string using C++ - 用 C++ 如何获取随机字符串
2009-07-26 13:56
756 查看
void CprjGetRandomStringTestDlg::OnBnClickedbtngetrandomstring() { CString strRandom; int i; bool bReturn; CString strMsg; for(i = 0;i < 100;i++) { bReturn = GetRandomString(strRandom,strMsg); if(bReturn) { TRACE(_T("%s/r/n"),strRandom); } else { } } } //Defination //bool GetRandomString(CString & strRandom, CString & strMsg, long lLengthMin = 5, long lLengthMax = 20, long lCharMin = 'a', long lCharMax = 'z'); bool CprjGetRandomStringTestDlg::GetRandomString(CString & strRandom, CString & strMsg, long lLengthMin, long lLengthMax, long lCharMin, long lCharMax) { int i; char * pcharRandom; long lLength; char charRandom; //=============================================================================================== if(lLengthMax < lLengthMin) { strMsg = _T("lLengthMax can't be less than lLengthMin"); return false; } else if(lLengthMin <= 0 || lLengthMax <= 0) { strMsg = _T("lLengthMax and lLengthMin can't be less than or equal 0"); return false; } //----------------------------------------------------------------------------------------------- if(lCharMax < lCharMin) { strMsg = _T("lCharMax can't be less than lCharMin"); return false; } else if(lCharMin <= 0 || lCharMax <= 0) { strMsg = _T("lCharMax and lCharMin can't be less than or equal 0"); return false; } //----------------------------------------------------------------------------------------------- //字符串长度范围 : 5 到 20(5 + (16 - 1)) 范围 lLength = rand() % (lLengthMax - lLengthMin + 1) + lLengthMin; //+1 : 多分配一个,最后一个字符必须是 /0 结束 pcharRandom = (char*)malloc(lLength + 1); memset(pcharRandom,0,lLength + 1); for(i = 0;i < lLength;i++) { //字符范围 : 97 到 122(97 + (26 - 1)) 范围 a - z charRandom = rand() % (lCharMax - lCharMin + 1) + lCharMin; pcharRandom[i] = charRandom; } //----------------------------------------------------------------------------------------------- CString strTemp(pcharRandom); strRandom = strTemp; //----------------------------------------------------------------------------------------------- free(pcharRandom); strTemp.Empty(); //----------------------------------------------------------------------------------------------- return true; }
相关文章推荐
- How to get random number within a range using C++ - 如何用C++获取一定范围内的随机数
- How to get the password text in a text with password property from another process using C++ - 用C++如何从不同进程获取密码框文本
- How to get the file's icon using C++ - 用C++如何获取文件的图标
- How to get the size of file in C++ - 在C++中如何获取文件的大小
- How to access database using C++ & ADO - 如何用C++和ADO访问数据库
- How to split string in C++(分割字符串)
- How to write event log(Event Viewer) using C++ - 用C++如何写事件日志(事件查看器)
- How to get local machine date format.如何获取本机时间格式
- 如何在C++中使用VBScript(使用Qt)--How to use VBScript in C++ (Using Qt)
- How to show a modal dialog and modeless dialog using C++ - 如何用C++显示一个模态对话框和一个非模态对话框
- how to truncate a string or get a substring ? js截取字符串
- python网络编程经典好文,如何用urllib获取internet资源,HOWTO Fetch Internet Resources Using The urllib Package
- 【获取、高亮UIWebView中选择的文本】How To Mark Or Get The Highlighted String Inside UIWebView
- How to invoke dll using C++ - C++ 如何调用DLL - 详解
- C#中如何获取其他进程的命令行参数 ( How to get other processes's command line argument )
- C++ How to avoid the enter key when using cin and getline
- How to change the background color of form using C++ - 如何用C++改变窗体的颜色
- question 002: dev c++ 当中如何调整字体大小?How to get the first program with C++? c++属于什么软件?
- How to get file extension from string in C++
- How to add icon for the item of ListCtrl using C++ (Mail Attachment ListCtrl) - 如何用C++为ListCtrl的Item添加图标(邮件附件ListCtrl)