Thread: How to pick a random character out of three characters

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    3

    How to pick a random character out of three characters

    In C-language, how can i pick a random character out of three characters. In those three characters there are both numbers and letters, for example 1Q2

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A quick search of this forum for "how to pick a random number" brings me to How to pick a random number between x and y. Adapt post #6 to your needs.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Abuse C array indexing rules for fun an profit : char x = "1Q2"[rand() % 3];

    Could easily be expanded to an arbitrary character array using a const char pointer and strlen() instead of hard-coding the array and length.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to pick a random number between x and y
    By CMEE in forum C Programming
    Replies: 5
    Last Post: 01-14-2012, 01:27 PM
  2. random characters
    By rebok in forum C Programming
    Replies: 5
    Last Post: 11-17-2004, 08:25 PM
  3. Random Pick from a file
    By mcorn in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2004, 03:53 PM
  4. Random Characters
    By C-Struggler in forum C Programming
    Replies: 4
    Last Post: 03-09-2003, 02:38 PM
  5. Pick character from a string
    By Yoshi in forum C++ Programming
    Replies: 1
    Last Post: 12-22-2001, 07:37 PM

Tags for this Thread