How to pick a random character out of three characters

This is a discussion on How to pick a random character out of three characters within the C Programming forums, part of the General Programming Boards category; In C-language, how can i pick a random character out of three characters. In those three characters there are both ...

  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
    22,157
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 04: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


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