日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

C语言随机字母生成,C++ 随机数字以及随机数字加字母生成的案例

發布時間:2023/11/27 生活经验 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C语言随机字母生成,C++ 随机数字以及随机数字加字母生成的案例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我就廢話不多說了,大家還是直接看代碼吧~

#include

#include

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;

補充知識:C/C++生成隨機數字符串(錯誤方法和正確方法)

先說錯誤的方法。生成的10個隨機數是一樣的。

#include

#include

#include

#include

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個不一樣的隨機數

#include

#include

#include

#include

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個隨機數

// printf("\n====%s====\n",str);

return 0;

}

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方歡迎留言討論,望不吝賜教。

總結

以上是生活随笔為你收集整理的C语言随机字母生成,C++ 随机数字以及随机数字加字母生成的案例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。