Random Value Generator with String Arrays in C#

/* Posted March 10th, 2008 at 1:00pm */
/* Filed under C# */

/* */

 The code below selects a random variable from an array of strings, demonstrating the random feature in c#

Code below…



_________________________________________________________________
protected void Page_Load(object sender, EventArgs e)

{
// Initialize the string array


string[] strStrings = { “Timmy”, “Edgar”, “Jason”, “Mike”, “Buck”, “Kelly” };

// Choose a random slogan

string person = randomPerson(strStrings);

Response.Write(person);
// Display the random slogan


}

public static string randomPerson (string[] people)

{
Random RandString = new Random();

string person = people[RandString.Next(0, people.Length)];

return person;

}
________________________________________________________________

  • Twitter
  • Digg
  • del.icio.us
  • Propeller
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Mixx
  • Yahoo! Buzz
  • NewsVine
  • Facebook
  • Google Bookmarks

Related Posts


Related Videos

Inheriting from Forms in… How to get Website Source… Write Code in Visual… Visual Studio

Related Stories

Related Tweets

Leave a Reply

or Login (not required)





HTML tags allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>