fastpig productions fastpig productions - 29 days ago 5
C# Question

How to easily randomize an array?

After my last question completely died I would like to ask a question:

How could I EASILY implement an array randomizer into my code:

private void button9_Click(object sender, EventArgs e)
{
axSkype1.CurrentUserProfile.MoodText = ();
}


This is my code and I'm using the skype4comlib so don't worry but I would like to find a way that I could set the users Skype 'mood' to a random element of an array such as "I am failing at coding, The Slow Brown Sheep jumped over the fox" yea oh and also I'm going to attach this to a 5 min timer but yea I can do that but not the array.

Answer
    private void button9_Click()
    {
        string messages = "I am failing at coding,The Slow Brown Sheep jumped over the fox,Message3,Message4, Message5";
        string[] messagesArray = messages.Split(new Char[] { ',' });
        axSkype1.CurrentUserProfile.MoodText =messagesArray[new Random().Next(0,messagesArray.Length)].ToString();
    }

or you can use a method to return a random string form a "messages" string:

    private void button9_Click()
    {
        string messages = "I am failing at coding,The Slow Brown Sheep jumped over the fox,Message3,Message4,Message5";
        axSkype1.CurrentUserProfile.MoodText =GetRandomMessage(messages);
    }

    string GetRandomMessage(string messages)
    {
        string[] messagesArray = messages.Split(new Char[] { ',' });
        return messagesArray[new Random().Next(0, messagesArray.Length)].ToString();
    }

or

Comments