int generateRandomNumber(int min,int max)
qsrand(QTime(0,0,0).msecsTo(QTime::currentTime()));
int num = qrand()%(max-min)+min;
qDebug()<<num;
return num;
3.生成不重复的随机数组
将产生的随机数存放到数组中,在每次产生新的随机数的同时,在随机数列表中检查是否包含该数
如果包含则重新生成,直到生成新的随机数为止。
int getRandom(int min,int max)
QList<int> list;
int i,j;
bool res = true;
qsrand(QTime(0, 0, 0).msecsTo(QTime::currentTime()));
for (int i=0; i<10; i++) {
int num = qrand()%(max-min)+min;
list.append(num);
while(res)
for(j=0;j<i;j++)
if(list[i]==list[j])
break;
if(j<i)
list[i] = qrand()%(max-min)+min;
if(j==i)
res=!res;
4.产生随机字符串
QString getRandomString(int length)
qsrand(QDateTime::currentMSecsSinceEpoch());
const char ch[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int size = sizeof(ch);
char* str = new char[length + 1];
int num = 0;
for (int i = 0; i < length; ++i)
num = rand() % (size - 1);
str[i] = ch[num];
QString res(str);
return res;