Probably in the future, I wrote a function to generate a random string. You can customize generation rules to generate string lengths. It imitates the function style of MS, generates rules using macros or rules, and returns values using Boolean. Boolean return may not be used here. Paste the Code directly. It is also two files.
# Pragma once
# Define rdalltype 0x07 // ''' D? D' o íê y ×?
# Define rdnum 0x01 // define Y x?
# Define rdlowercase 0x02 // D? D ′
# Define rduppercase 0x04 ′
Static tchar szallchar [] = _ T ("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 ");
Bool fnrandomstring (const int nleng, tchar * randstringinput, const int ntype );
// The annotation uses Chinese characters, and the plug-in pasting does not support Chinese characters well:
# Define rdalltype 0x07 // case and number
# Define rdnum 0x01 // number
# Define rdlowercase 0x02 // lower case
# Define rduppercase 0x04 // uppercase
#include "stdafx.h"
#include "RandomString.h"
#include <stdlib.h>
#include <time.h>
bool fnRandomString(const int nLeng, TCHAR *RandStringInput, const int nType)
{
if (0 == nLeng)
{
return true;
}
if (nLeng < 0)
{
return false;
}
srand((unsigned int)time(NULL));
bool bRet = false;
int nMin = 0;
int nMax = 62;
switch(nType & RdAllType)
{
case RdAllType:
break;
case RdNum:
nMin = 52;
break;
case RdNum | RdLowercase:
nMin = 26;
break;
case RdNum | RdUppercase:
nMin = 26;
break;
case RdUppercase:
nMax = 25;
break;
case RdUppercase | RdLowercase:
nMax = 51;
break;
case RdLowercase:
nMin = 26;
nMax = 51;
break;
default:
return false;
break;
}
int nRange = nMax - nMin;
if (nType == (RdNum | RdUppercase))
{
int nNum = 0;
for (int t = 0; t <= nLeng; t++)
{
nNum = nMin + rand()%nRange;
if (nNum < 52)
{
nNum -= 26;
}
RandStringInput[t] = szAllChar[nNum];
}
}
else
{
for (int t = 0; t <= nLeng; t++)
{
RandStringInput[t] = szAllChar[nMin + rand()%nRange];
}
}
//int tt = 0;
//while (RandStringInput[tt])
//{
// printf("%c ", RandStringInput[tt]);
// tt++;
//}
return true;
}
Compiled in vs2010.