Thread: need a random word from a txt file

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    7

    need a random word from a txt file

    hi i got this problem here where i need the string to
    be a random string out of a text file full of strings
    it just grabs the first one

    how to make to it takes a random one out of all of them



    Code:
    string readtxtfile = File.ReadAllText("Nicknames.txt");
    i tried lots of stuff and nothing as worked so far

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Random Class (System)

    Have a read.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    7
    i get the part of how to read the file i just dont get how to make it random

  4. #4
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    It just grabs one of them? The only way that would be true, if you are using File.ReadAllText("Nicknames.txt");, is if there is only one word in the file. Because the entire thing is being read and put into that string variable. So I don't understand this...

    How are the words delimited? And how large is this file? These are important questions that will influence my final suggestion.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Announcements - C++ Programming
    9. Users are allowed only one account unless they are given special permission to hold multiple accounts. Users may not share their account or password with others.

    Basically the same question, and from the same IP as this poster -> question about how to chose a random word

    Choose which account you want to keep, and use it from now on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    7
    Quote Originally Posted by cstryx View Post
    It just grabs one of them? The only way that would be true, if you are using File.ReadAllText("Nicknames.txt");, is if there is only one word in the file. Because the entire thing is being read and put into that string variable. So I don't understand this...

    How are the words delimited? And how large is this file? These are important questions that will influence my final suggestion.
    well its a text fille the one u get when u right click on desktop

    the file loook like this


    word1
    word2
    word3


    with no space at the end of them

    basicily it use word1 as the sttring

    none of the other words are implicated

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    This is so simple in C# as to be ridiculous to ask about it. Its actually pretty easy in C++ come to think of it.

    1. Read file into array or buffer.
    2. Parse words into second array (words are separated by spaces)
    3. Choose random index in second array.
    4. Display word at random index

    In .NET this is cake b/c there is a massive library sitting there waiting to be used.

  8. #8
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Quote Originally Posted by VirtualAce View Post
    This is so simple in C# as to be ridiculous to ask about it. Its actually pretty easy in C++ come to think of it.

    1. Read file into array or buffer.
    2. Parse words into second array (words are separated by spaces)
    3. Choose random index in second array.
    4. Display word at random index

    In .NET this is cake b/c there is a massive library sitting there waiting to be used.
    Not in this case, the words are separated by a newline char not a space. abbylennon said:

    the file loook like this


    word1
    word2
    word3


    with no space at the end of them
    And depending on the size of the file you might not want to read the thing into an array, unless you mean for the buffer to be read in chunks? Why would you need to parse the file into a second array though? That would be a waste of memory.
    Last edited by cstryx; 04-28-2013 at 05:11 PM.

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    7
    i have a 15 mb file with words in it what is the best way to select a random name in it without the app crashing on startup because of memory overload

  10. #10
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    15mb in memory isn't too bad... Webbrowsers use around 200-400mb on average for me. But you're probably also not going to be using that memory the whole time, and just to select a word, so File.ReadAllLines() should be fine. Then use the Random class to grab a random word from a line based on the index of the string array returned by the function. 2147483647 should be large enough to reach all indexed words in the array. Just make sure that you set your Min and Max values accordingly.

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    7
    any suggestion for the random class
    one best suited to enter that value

    thx alot

  12. #12
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    There is only one Random class in the .NET framework. There's other classes that can be utilized to get a random output, but other than that... I'm not sure what you're asking?

  13. #13
    Registered User
    Join Date
    Apr 2013
    Posts
    7
    Quote Originally Posted by cstryx View Post
    There is only one Random class in the .NET framework. There's other classes that can be utilized to get a random output, but other than that... I'm not sure what you're asking?
    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

  14. #14
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Code:
    r.Next(0, lines.Length - 1);
    Already wrong here. Read the MSDN documentation for the Next() function. The upperbound value is an exclusive value, so if you had it set to lines.Length, this would mean that it would never return a value equal to or higher than lines.Length. The way you have it now, the word in the last line will never be chosen.

    Also, look at how the Random is seeded. You might want to make this a member variable as a static instance of the class.

    Other than that, you're on the right track...

  15. #15
    Registered User
    Join Date
    Apr 2013
    Posts
    7
    Quote Originally Posted by cstryx View Post
    Code:
    r.Next(0, lines.Length - 1);
    Already wrong here. Read the MSDN documentation for the Next() function. The upperbound value is an exclusive value, so if you had it set to lines.Length, this would mean that it would never return a value equal to or higher than lines.Length. The way you have it now, the word in the last line will never be chosen.

    Also, look at how the Random is seeded. You might want to make this a member variable as a static instance of the class.

    Other than that, you're on the right track...
    so where do i put that number 2147483647

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