Thread: need a random word from a txt file

  1. #16
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Quote Originally Posted by abbylennon View Post
    i got it to work

    i used this
    var lines = File.ReadAllLines(path);var r = new Random();
    var randomLineNumber = r.Next(0, lines.Length - 1);
    var line = lines[randomLineNumber];

    do u think its the best method suited for my need

    its still made my app crash but less fast with that one
    * cringes at the over-use of the "var" keyword. *

  2. #17
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Quote Originally Posted by abbylennon View Post
    so where do i put that number 2147483647
    Nowhere. You should be reading what I post more clearly...

    The random function returns an integer, the max value for a 32 bit integer is 2147483647, thus, if you had more than 2147483647 indices, you could not reference a word at index 2147483648 now could you? It was an added bit of info I put in that really has no meaning here since you're not dealing with that many words.

    Quote Originally Posted by theoobe View Post
    * cringes at the over-use of the "var" keyword. *
    There's nothing bad about it except for when you're trying to read the code later on and you have to look at what the types are. It doesn't do any disadvantage otherwise.

  3. #18
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by cstryx View Post
    There's nothing bad about it except for when you're trying to read the code later on and you have to look at what the types are. It doesn't do any disadvantage otherwise.
    overuse of var often means that you don't understand what's actually going on. it makes it harder for others to read, and even makes it harder for you to read if you have to come back to it weeks or months later.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #19
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Quote Originally Posted by Elkvis View Post
    overuse of var often means that you don't understand what's actually going on. it makes it harder for others to read, and even makes it harder for you to read if you have to come back to it weeks or months later.
    "overuse of var often means that you don't understand what's actually going on" - Not necessarily.

    "it makes it harder for others to read, and even makes it harder for you to read if you have to come back to it weeks or months later" - Yes, this is what I initially mentioned.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 10-23-2011, 07:17 PM
  2. Random Word Generator? Plz Help
    By jjwballer in forum C Programming
    Replies: 7
    Last Post: 12-09-2010, 04:09 PM
  3. reading text-and-numbers file word by word
    By bored_guy in forum C Programming
    Replies: 22
    Last Post: 10-26-2009, 10:59 PM
  4. Random selection of a word
    By doobyscoo in forum C Programming
    Replies: 1
    Last Post: 04-14-2003, 11:39 AM
  5. open file, search of word, replace word with another
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 06-05-2002, 01:16 PM