Random letters

This is a discussion on Random letters within the C++ Programming forums, part of the General Programming Boards category; How would I use rand() to use letters other than numbers? I only want a-z and the first letter to ...

  1. #1
    Unregistered
    Guest

    Random letters

    How would I use rand() to use letters other than numbers? I only want a-z and the first letter to be capitol.

  2. #2
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    Munich, Germany
    Posts
    224
    char unknown = (rand() % 26) + 'a';
    putc(unknown);

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Don't forget to seed the random number generator with srand(...);
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    you just have to get a random number between 0 and 255 for a signed char. -128 and 127 for unsigned (are those numbers right?).

  5. #5
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    just make it come up with a random number from 1 to 26. But I guess I'm late, aren't I?

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    178
    i'm sure one of these other replys would work better but a really simple way to do it is to use

    Code:
    #include<stdio.h>
    #include<iostream.h>
    
    int main()
    
    int mynumber;   // declare the variable
    char myletter[2];     
    
    
    //can't think of the code but srand() would go here
    mynumber= rand()%25 + 1;   //get a random number between 1 and 26
    
    switch(mynumber)
    {
         case 1:
         {
                strcpy("A" , myletter);    copy 'A' into myletter
                break;
          }    //end case
          case 2:
          {
                 strcpy("B", myletter);
                 break;
           }
    
    }  //end switch     
    
    return 0;
    
    }//end main
    once again this code is probably the simplest and most ineffecient way to do this but should work. Hoipe i was of some help.
    Oi Oi Oi!!!!

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    oh lord, that is rather inefficient ... no use in using switch ... and instead of using an array, use just one char ...

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    something like this?

    if you want the first letter to be capitalized i assume you are looking for a completely random word?

    void randomWord(char *word, int length)
    {
    word[0] = (rand() % 26) + 'A';

    for(int i=1; i < length; i++)
    word[i] = (rand() % 26) + 'a';
    }

  9. #9
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    you could store letters and numbers or what ever in an array...

    Code:
    char randstuff[5] = {'A', '>', '2', 'r', 'R', '6'}
    then get a random number between 0 and 6 is it?

    any way assign a variable like `i' or some thing to the random number

    then
    Code:
    cout << randstuff[i];

    note: forgive me... im half asleep...
    What is C++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating random letters
    By ominub in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 01:12 AM
  2. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 04:51 PM
  3. Random letters
    By CAP in forum C Programming
    Replies: 19
    Last Post: 07-12-2002, 05:55 PM
  4. generate random letters
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-19-2002, 02:22 AM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21