Skip to content

Instantly share code, notes, and snippets.

@songtianlun
Last active September 18, 2020 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save songtianlun/651f6172ec2019af0f70148f69664155 to your computer and use it in GitHub Desktop.
Save songtianlun/651f6172ec2019af0f70148f69664155 to your computer and use it in GitHub Desktop.
Generate random string of specified length.
/*************************************************************************
> 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