Thread: Random character

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    9

    Random character

    How may I get a random character from A to F?
    I've tried static_cast<char>(65 + rand() % 70) and it doesn't works.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    char foo = 'A' + rand() % ('F'-'A');

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

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Think ya need to add 1 to that:
    char foo = 'A' + rand() % (1 + 'F' - 'A');
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Zach L.
    Think ya need to add 1 to that:
    char foo = 'A' + rand() % (1 + 'F' - 'A');
    Right you are. That'll teach me to talk and type at the same time.

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

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Originally posted by Salem
    How about
    Code:
    "ABCDEF"[rand()%6];
    Amazing, thanks. Never thought about doing random stuff like that. In fact, you can place whatever you want in that string and it'll work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf returns random character
    By giannisapi in forum C Programming
    Replies: 3
    Last Post: 06-20-2009, 12:06 PM
  2. character set translation
    By password636 in forum C Programming
    Replies: 1
    Last Post: 06-08-2009, 11:45 AM
  3. get wide character and multibyte character value
    By George2 in forum C++ Programming
    Replies: 27
    Last Post: 01-27-2008, 05:10 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM