question about how to chose a random word

This is a discussion on question about how to chose a random word within the C# Programming forums, part of the General Programming Boards category; hi i got this code here id like for it to pick up a random word from a text file ...

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

    question about how to chose a random word

    hi i got this code here id like for it to pick up a random word from a text file and use it as the nick in this line


    Code:
    SetNick(GetRandomString(10));



    how could i do it heres the full code for the client


    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using FluorineFx;
    using FluorineFx.Messaging.Adapter;
    using FluorineFx.Messaging.Api.Service;
    using FluorineFx.Net;
    using System.Threading;
    using System.IO;
    
    
    
    
    namespace TinyChat_SPam
    {
    class Client : IPendingServiceCallback
    {
    private static Random rand = new Random();
    static string[] colors = { "#1965b6", "#32a5d9", "#7db257", "#a78901", "#9d5bb5", "#5c1a7a", "#c53332", "#821615", "#a08f23", "#487d21", "#c356a3" };
    static int colorIndex = 0;
    
    
    
    
    public NetConnection mNetConnection;
    private Thread thdHandler;
    
    
    
    
    public string roomName;
    public bool FlagDone = false;
    
    
    
    
    public const string MsgPath = "message.txt";
    
    
    
    
    public Client()
    {
    }
    
    
    
    
    public Client(string roomName)
    {
    mNetConnection = new NetConnection();
    mNetConnection.OnConnect += OnConnect;
    mNetConnection.OnDisconnect += OnDisconnect;
    this.roomName = roomName;
    }
    
    
    
    
    public void SetHandler(Thread thdHandler)
    {
    this.thdHandler = thdHandler;
    }
    
    
    
    
    private void OnConnect(object sender, EventArgs e)
    {
    if (mNetConnection.Connected)
    {
    Console.WriteLine("Connected.");
    Thread.Sleep(200);
    SetNick("O" + GetRandomString(10) + "G");
    
    
    
    
    Thread.Sleep(200);
    foreach (string msg in File.ReadAllLines(MsgPath))
    {
    SendMessage(new Message(msg));
    Thread.Sleep(200);
    }
    }
    FlagDone = true;
    }
    
    
    
    
    private void OnDisconnect(object sender, EventArgs e)
    {
    Console.WriteLine("Disconnected.");
    thdHandler.Abort();
    }
    
    
    
    
    private void SetNick(string name)
    {
    mNetConnection.Call("nick", this, name);
    }
    
    
    
    
    private void SendMessage(Message message)
    {
    colorIndex++;
    
    
    
    
    if (colorIndex >= colors.Length)
    {
    colorIndex = 0;
    }
    string tmp = message.Format(',');
    string[] param = new string[] { tmp, colors[colorIndex] + ",en" };
    mNetConnection.Call("privmsg", this, param);
    }
    
    
    
    
    private string GetRandomString(int length)
    {
    Thread.Sleep(25);
    rand = new Random(DateTime.Now.Millisecond / new Random().Next(2, 5));
    string tmpString = "";
    while (length-- > 0)
    {
    tmpString += (char)rand.Next('a', 'z');
    }
    
    
    
    
    return tmpString;
    }
    
    
    
    
    public void ResultReceived(IPendingServiceCall call)
    {
    throw new NotImplementedException();
    }
    }
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    Also here -> Help with random strins - Dev Shed
    And here -> http://forums.codeguru.com/showthrea...text-file-in-c
    And OMG, more -> http://www.cplusplus.com/forum/general/100334/


    Why don't you start with
    Code:
    public class Hello1
    {
    
        private string GetRandomString(int length)
        {
            Thread.Sleep(25);
            rand = new Random(DateTime.Now.Millisecond / new Random().Next(2, 5));
            string tmpString = "";
            while (length-- > 0)
            {
                tmpString += (char)rand.Next('a', 'z');
            }
            return tmpString;
        }
    
       public static void Main()
       {
          System.Console.WriteLine("Random name=" + GetRandomString(10) );
       }
    }
    And tell us what it prints, or what your error message(s) are.

    Mind you, a class name containing "chat" and "spam" is not exactly endearing towards honest motives for wanting this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    Code:
    heres the result
    
    
    
    {~
    
    
        private string GetRandomString(int length)
        {
            Thread.Sleep(25);
            rand = new Random(DateTime.Now.Millisecond / new Random().Next(2, 5));
            string tmpString = "";
            while (length-- > 0)
            {
                tmpString += (char)rand.Next('a', 'z');
            }
            return tmpString;
        }
    
    
       public static void Main()
       {
          System.Console.WriteLine("Random name=" + GetRandomString(10) );
       }
    }~
    
    with two errors anototed by ~ 
    
    
    the start { and end }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    Exactly how much C# experience do you have?

    Can you even write the "hello world" example yourself and make it work?
    From there, it's just a simple matter to paste in your GetRandomString() and make that work as well.

    I'm not entirely surprised that the code I posted didn't work (I don't know C# at all), but it should have been enough to inspire and guide you in the right direction.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    thx alot salem for you reply
    that alone makes me realy happy

    im a begginer

    i was wondering based on the initial code
    if i could have the nickname to be picked out of a .txt file

    say the first word thats on the list
    Last edited by toadperry; 04-26-2013 at 12:46 PM.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    I'm hesitant to answer this question. You seem to be in [i]way[i/] over your head and, as Salem pointed out, your intentions with this application are questionable.

    I'd recommend tackling an easier problem to start with. Maybe some tutorials.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    hi i was able to make the characther lenght random but i still havent figuered how to chose a random nickname from a txt file anyone know how to do that at all

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    A development process

    It's the same approach, regardless of whatever language you're programming in.

    > but i still havent figuered how to chose a random nickname from a txt file anyone know how to do that at all
    Using your test "hello world" program, can you
    - just read and print a file (at all)
    - read a file as words
    - read a file as words, and store words in the array
    - print the array when you finished reading the file

    If you get to the end of that list, then picking a random subscript in the array is the final step.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    46
    Random word from a text file? How are the words delimited? By line, char? How big is the file itself also?

    The usage of Thread.Sleep() from what I seen in this thread is also really disturbing... :S

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. picking out a random word as a nickanme
    By toadperry in forum C++ Programming
    Replies: 3
    Last Post: 04-26-2013, 12:35 PM
  2. Replies: 4
    Last Post: 03-09-2013, 09:30 AM
  3. Random Word Generator? Plz Help
    By jjwballer in forum C Programming
    Replies: 7
    Last Post: 12-09-2010, 03:09 PM
  4. Random word problem
    By goron350 in forum C++ Programming
    Replies: 2
    Last Post: 05-14-2005, 03:44 PM
  5. Random selection of a word
    By doobyscoo in forum C Programming
    Replies: 1
    Last Post: 04-14-2003, 11:39 AM

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