C++ 隨機(jī)數(shù)字以及隨機(jī)數(shù)字加字母生成的案例
更新時(shí)間:2020年12月10日 16:55:51 作者:wjbooks
這篇文章主要介紹了C++ 隨機(jī)數(shù)字以及隨機(jī)數(shù)字加字母生成的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
我就廢話不多說了,大家還是直接看代碼吧~
#include <time.h> #include <sys/timeb.h> void MainWindow::slot_clicked() { QString strRand; int length = 32; QString strTmp = "1234567890QWERTYUIOPASDFGHJKLZXCVBNM"; struct timeb timer; ftime(&timer); srand(timer.time * 1000 + timer.millitm);//毫秒種子 for(int i = 0; i < length; i++ ) { int j = rand()%35; strRand += strTmp.at(j); } qDebug() << strRand;
補(bǔ)充知識(shí):C/C++生成隨機(jī)數(shù)字符串(錯(cuò)誤方法和正確方法)
先說錯(cuò)誤的方法。生成的10個(gè)隨機(jī)數(shù)是一樣的。
#include <stdio.h> #include <time.h> #include <stdlib.h> #include <string.h> void make_rand_str(char *pchStr,int iLen) { time_t tCurTime = 0; int iRandValue = 0; int i = 0; unsigned int state = 0; if(NULL == pchStr || iLen <= 0) { return; } tCurTime = time(NULL); printf("\n***%ld***%u**\n",tCurTime ,(unsigned int)tCurTime); srand((unsigned int)tCurTime); iRandValue = rand(); snprintf(pchStr,iLen,"%d",iRandValue); printf("\n====%s====\n",pchStr); return; } int main() { char str[20]; int i = 0; for(i = 0; i < 10; i++) { memset(str,0,sizeof(str)); make_rand_str(str,sizeof(str)); // printf("\n====%s====\n",str); } return 0; }
正確的方法,生成了10個(gè)不一樣的隨機(jī)數(shù)
#include <stdio.h> #include <time.h> #include <stdlib.h> #include <string.h> void make_rand_str(char *pchStr,int iLen,int num) { time_t tCurTime = 0; int iRandValue = 0; int i = 0; unsigned int state = 0; if(NULL == pchStr || iLen <= 0) { return; } tCurTime = time(NULL); printf("\n***%ld***%u**\n",tCurTime ,(unsigned int)tCurTime); srand((unsigned int)tCurTime); for(i = 0;i < num; i++) { iRandValue = rand(); snprintf(pchStr,iLen,"%d",iRandValue); printf("\n====%s====\n",pchStr); } return; } int main() { char str[20]; memset(str,0,sizeof(str)); make_rand_str(str,sizeof(str),10); // 10個(gè)隨機(jī)數(shù) // printf("\n====%s====\n",str); return 0; }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方歡迎留言討論,望不吝賜教。
您可能感興趣的文章:
- c++實(shí)現(xiàn)簡(jiǎn)單隨機(jī)數(shù)的代碼
- c++ 隨機(jī)數(shù)問題的相關(guān)研究
- C++生成隨機(jī)數(shù)的實(shí)現(xiàn)代碼
- C++編程產(chǎn)生指定范圍內(nèi)的隨機(jī)數(shù)
- C++實(shí)現(xiàn)產(chǎn)生隨機(jī)數(shù)和相應(yīng)的猜拳小游戲?qū)嵗a
- C++ 隨機(jī)數(shù)與隨機(jī)種子數(shù)的實(shí)例
- C++編寫生成不重復(fù)的隨機(jī)數(shù)代碼
- C/C++產(chǎn)生指定范圍和不定范圍隨機(jī)數(shù)的實(shí)例代碼
- C語言/C++中如何產(chǎn)生隨機(jī)數(shù)
- C++產(chǎn)生隨機(jī)數(shù)的實(shí)現(xiàn)代碼
- C++11生成隨機(jī)數(shù)(random庫)的使用
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(557.翻轉(zhuǎn)字符串中的單詞之三)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(557.翻轉(zhuǎn)字符串中的單詞之三),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08C++從匯編的視角審視對(duì)象的創(chuàng)建問題
這篇文章主要介紹了C++從匯編的視角看對(duì)象的創(chuàng)建,從匯編的視角來看,調(diào)用構(gòu)造器和調(diào)用 “返回對(duì)象” 的函數(shù)是一樣的,從匯編的角度來看,對(duì)象就是一堆數(shù)據(jù)的排列,比如說最普通的對(duì)象就是數(shù)據(jù)成員按照聲明順序直接排列,需要的朋友可以參考下2022-01-01C語言模擬實(shí)現(xiàn)字符串庫函數(shù)的示例講解
這篇文章主要為大家詳細(xì)介紹了C語言模擬實(shí)現(xiàn)字符串庫函數(shù)的具體方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01