Last active
September 18, 2020 12:54
-
-
Save songtianlun/651f6172ec2019af0f70148f69664155 to your computer and use it in GitHub Desktop.
Generate random string of specified length.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************************************* | |
> Function : Generate random string of specified length. | |
> Author : TL Song | |
> EMail : songtianlun@frytea.com | |
> Created Time : Fri 18 Sep 2020 11:52:32 AM CST | |
************************************************************************/ | |
void RandomString(char * str, int len) | |
{ | |
int RandomAscii; | |
memset(str, 0, len); | |
srand((unsigned)time(NULL)); | |
for(int i = 0;i < len; i++) | |
{ | |
RandomAscii = rand() % 93 + 33; | |
//sprintf(str + i, "%x", RandomAscii ); | |
*(str + i) = RandomAscii; | |
//std::cout << "Random Ascii : " << RandomAscii << " Char: " << (str + i) << std::endl; | |
if ( i == len - 1) | |
{ | |
* ( str + i ) = '\0'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment