//-------------------------------------------------------------------
//-- Random Password Generator C++ sample - davemoats.com
//--
//-- This sample is provided 'as-is', without any express or implied warranty.
//-- In no event will the authors be held liable for any damages arising from
//-- the use of this sample code.
//--
//-------------------------------------------------------------------

//-----------------------------------
// define to whatever length you need
//-----------------------------------

const int PwdLen = 8;

//-----------------------------------
// place holder for the password
//-----------------------------------

char szPswd[10] = { NULL };

//-----------------------------------
// seed the (pseudo) random number generator
//-----------------------------------

srand( (unsigned)time( NULL ) );
// or something like
// SYSTEMTIME st;
// GetLocalTime( &st );
// srand( st.wSeconds );


//-----------------------------------
// now generate the password
//-----------------------------------

for( int i=0; i<iPwdLen; )
{

        //-----------------------------------
        // now generate a random integer
        //-----------------------------------

        int iRanNum = rand();

        //-----------------------------------
        // throw away non-standard characters
        //-----------------------------------

        if(( iRanNum < 127 ) && ( iRanNum > 31 ))
        {

                szPswd[i] = ranNum;
                i++;

        }

}

szPswd[i] = '\0';

//-----------------------------------
// now store your newly created password somewhere
//-----------------------------------




Copyright © 2005 - 2013 Dave Moats.  All rights reserved.  Links: Copyright © by their respective owners.
NO WARRANTIES EXTENDED.  Void where prohibited by law.  Please report any issues or broken links.
You may link to this site freely from your own site.  You may quote from this site, but please include a link to the original source on the originating site.