Thread: Generate A Random Keyword (String)

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    7

    Generate A Random Keyword (String)

    Good Day Everyone.

    I am currently trying to generate a random 4 digit string. (Ex: azhs)

    I know that I could use a random number between 1 and 26 and then do a check to assign a character to that number.

    I was just wondering if there is a easier, yet still effective way of doing this.

    Thank you.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int x;
    char word[5] = {0};
    
    for( x = 0; x < 4; x++ )
        word[x] = rand()%26 + 'a';
    Or, if you don't like that:
    Code:
    char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
    char word[5] = {0};
    int x;
    
    for( x = 0; x < 4; x++ )
        word[x] = alphabet[ rand()%26 ];

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    7
    Thank you very much quzah.

    Much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM