Random String Generator

For a verification Key I needed a random string, which I was implementing like this:
// iType:   0 -> all syblos allowed
//          1 -> alphanumeric
//          2 -> alpha
//          3 -> numeric
CString RandomString( int iLength, int iType )
{
    CString strReturn;
    CString strLocal;

    for( int i = 0 ; i < iLength ; ++i )
    {
        int iNumber;

        // Seed the random-number generator with TickCount so that
        // the numbers will be different every time we run.
        srand( (unsigned int)( (i+1)*iLength*GetTickCount() ) );

        switch( iType )
        {
            case 1:
                iNumber = rand()%122;
                if( 48 > iNumber )
                    iNumber += 48;
                if( ( 57 < iNumber ) &&
                    ( 65 > iNumber ) )
                    iNumber += 7;
                if( ( 90 < iNumber ) &&
                    ( 97 > iNumber ) )
                    iNumber += 6;
                strReturn += (char)iNumber;
                break;
            case 2:
                iNumber = rand()%122;
                if( 65 > iNumber )
                    iNumber = 65 + iNumber%56;
                if( ( 90 < iNumber ) &&
                    ( 97 > iNumber ) )
                    iNumber += 6;
                strReturn += (char)iNumber;
                break;
            case 3:
                strLocal.Format("%i", rand()%9 );
                strReturn += strLocal;
                break;
            default:
                strReturn += (char)rand();
                break;
        }
    }

    return strReturn;
}

Bugs and Improvements:

Please report all bugs and improvements to me, thanks and enjoy it.

Last updated: 12 June 1998




Comments

  • Thank you

    Posted by rm_pkt on 03/27/2006 05:57am

    Nice code. I really enjoyed it

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds