Thread: pick randomly from array

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    pick randomly from array

    Hey

    I'm doing a hangman project at the moment,I have a string array containing 10 words to be guessed.I can generate a random number between 0 and 9 but how do i compare this to the index of the elements to randomly pick a word???

    thanks

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Since you can pick a random number in the right range, just use THAT as the index into the array.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I am not sure what is the question. Don't you already have a random index that you can immediately use?
    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

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    Ok,i have an array with 10 words and i want to pick one of them randomly,how do i do it

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You would "generate a random number between 0 and 9" and then use that number as the index of the randomly chosen element.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pick data from file store in array
    By swgh in forum C Programming
    Replies: 1
    Last Post: 07-10-2009, 09:57 AM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM