這個需求也是最近乙個專案裡的實際需求。測試乙個資料庫相關的**。要往資料庫裡隨機的填寫一些字段。
從原理上是很容易實現。建立乙個陣列,存放一些字元。然後隨機的從裡面取一些字元湊成乙個字串。
所以這個**沒啥可說的。裡面用到了 std::random_device,std::mt19937 ,std::uniform_int_distribution 可以作為 c++ 生成隨機數的乙個例子。還用到了初始化列表來初始化乙個 qvector , 這個也可以作為乙個例子。
#ifndef randomstring_h
#define randomstring_h
#include
#include
/*** @brief the randomstring class 用來生成隨機字串,可以指定隨機字串裡包含哪些字元
* 用法舉例: randomstring rng;
* rng.setcharset("0123456789abcdef");
* qstring rng.randn(10);
*/class
randomstring
;void
addcharset
(charset set)
;void
addcharset
(qstring set)
;private
: qvector m_charset;
static qvector m_number;
static qvector m_lowercase;
static qvector m_uppercase;
std::random_device randomdevice;
std::mt19937 randomgenerator;};
#endif
// randomstring_h
#include
"randomstring.h"
#include
qvector randomstring::
m_number()
; qvector randomstring::
m_lowercase()
; qvector randomstring::
m_uppercase()
;
randomstring::
randomstring()
:randomgenerator
(randomdevice()
)
randomstring::
randomstring
(unsigned
int seed)
:randomgenerator
(seed)
void randomstring::
addcharset
(qstring set)
}
qstring randomstring::
randn
(int n)
return ret;
}void randomstring::
resetcharset()
void randomstring::
addchar
(qchar c)
void randomstring::
addcharset
(qvector set)
void randomstring::
addcharset
(charset set)
}
C 生成隨機字串
生成隨機字串 目標字串的長度 是否包含數字,1 包含,預設為包含 是否包含小寫字母,1 包含,預設為包含 是否包含大寫字母,1 包含,預設為包含 是否包含特殊字元,1 包含,預設為不包含 要包含的自定義字元,直接輸入要包含的字元列表 指定長度的隨機字串 public static string ge...
C 生成隨機字串
1.是 的,此處記錄一下。生成隨機字串 目標字串的長度 是否包含數字,1 包含,預設為包含 是否包含小寫字母,1 包含,預設為包含 是否包含大寫字母,1 包含,預設為包含 是否包含特殊字元,1 包含,預設為不包含 要包含的自定義字元,直接輸入要包含的字元列表 指定長度的隨機字串 public sta...
生成隨機字串
原文出處 估摸著以後極有可能使用到,於是寫了乙個生成隨機字串的函式。可以自定義生成規則,生成字串長度。模仿了ms的函式風格,生成規則使用巨集的或且規則,返回值使用了布林型。這裡使用布林返回可能作用不大。直接貼 吧,同樣是兩個檔案。randomstring.h pragma once define r...