Generate random string

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.