Make a Shakespeare insult generator in C#

[Shakespeare insult generator]

This example shows how to build a Shakespeare insult generator by selecting one word from each of three arrays and combining the results. The following code shows the arrays (with many values omitted to save space).

// The insult pieces
// (from
private string[] Column0 =

private string[] Column1 =

private string[] Column2 =

When you click the Generate button, the following code generates a random insult.

// Generate a random insult.
private void btnGenerate_Click(object sender, EventArgs e)
    txtInsult.Text = "Thou " +
        Column0.RandomElement() + " " +
        Column1.RandomElement() + " " +

To implement the Shakespeare insult generator, this code code uses the RandomElement extension method described in the post Make extension methods that pick random items from arrays or lists in C#. It uses that method to pick random items from each of the three arrays and combines the selections to generate the insult. It then displays the result.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in arrays, miscellany, strings and tagged , , , , , , , , , , , , , , . Bookmark the permalink.