CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2008
    Location
    India
    Posts
    780

    Random string generation not working fine?

    Hi all,

    i m generating a random string,but its give same result when i generate these.

    means i generate 5 different string than each and every time these 5 string are same as previously generated.
    Code:
    CString Generate_Str()
    {
    	int		m_iLength=5;
    	BOOL	m_bLowercase=true;
    	BOOL	m_bNumbers=true;
    	BOOL	m_bSymbols=false;
    	BOOL	m_bUppercase=true;
    	CString	String="";
     
    	
     
    		int gen_len;  
    		CString pword="";
    		char possibles[77] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-+.,";
     
    		for ( gen_len=0; gen_len < m_iLength; gen_len++ )
    		{
    			int todo;
     
    			bool decided = false;
    			do
    			{
    				int rand_me; 
    				rand_me = rand() % 4;
     
    				if ( (rand_me == 0) && (m_bLowercase == TRUE) ){
    						todo = 0;
    						decided = TRUE;
    					}
    					if ( (rand_me == 1) && (m_bUppercase == TRUE) ){
    						todo = 1;
    						decided = TRUE;
    					}
    					if ( (rand_me == 2) && (m_bNumbers == TRUE) ){
    						todo = 2;
    						decided = TRUE;
    					}
    					if ( (rand_me == 3) && (m_bSymbols == TRUE) ){
    						todo = 3;
    						decided = TRUE;
    					}
     
    			}while ( decided != TRUE );
    			
     
    			char value='a';
    			switch (todo) 
    			{
    				case 0 :
    					//Lower Letters
    					value = possibles[ (rand() % 26) ];
    					break;
    				case 1 :
    					//Cap Letters
    					value = possibles[ (rand() % 26) + 26 ];
    					break;
    				case 2 :
    					//Numbers
    					value = possibles[ (rand() % 10) + 52 ];
    					break;
    				case 3 :
    					//Symbols
    					value = possibles[ (rand() % 14) + 63 ];
    					break;
    				default:
    					
    					break;
    			}
    			pword = pword + value;
    		String= pword;
    	
     
    	return String;
    }
     
    //Alwayz give this
    //=================
    HOU84
    NFX7V
    CGIK1
    9F33K
    PEGRV
    i want each time generate a new and different string.

    please help me for this.

    thanks in advance.
    IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH

  2. #2
    Join Date
    Mar 2008
    Location
    Turin / Italy
    Posts
    178

    Re: Random string generation not working fine?

    have you used srand() at the top of the program?

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Random string generation not working fine?

    Also please note that rand() isn't really suitable for cryptographic purposes (unless it's toy cryptography) what it looks like you want to use it for. There are better alternatives both in the Windows API (IIRC) and the C++0x standard library extensions.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured