41

I'm looking for a c# generator which can generate random words, sentences, paragraphs given by a number of words / paragraphs and certain syntax such as Address, numbers, postal code / zip code, country, phone numbers, email address.

alex
  • 614
  • 1
  • 6
  • 15

10 Answers10

68
static string LoremIpsum(int minWords, int maxWords,
    int minSentences, int maxSentences,
    int numParagraphs) {

    var words = new[]{"lorem", "ipsum", "dolor", "sit", "amet", "consectetuer",
        "adipiscing", "elit", "sed", "diam", "nonummy", "nibh", "euismod",
        "tincidunt", "ut", "laoreet", "dolore", "magna", "aliquam", "erat"};

    var rand = new Random();
    int numSentences = rand.Next(maxSentences - minSentences)
        + minSentences + 1;
    int numWords = rand.Next(maxWords - minWords) + minWords + 1;

    StringBuilder result = new StringBuilder();

    for(int p = 0; p < numParagraphs; p++) {
        result.Append("<p>");
        for(int s = 0; s < numSentences; s++) {
            for(int w = 0; w < numWords; w++) {
                if (w > 0) { result.Append(" "); }
                result.Append(words[rand.Next(words.Length)]);
            }
            result.Append(". ");
        }
        result.Append("</p>");
    }

    return result.ToString();
}
Chris Ballance
  • 33,810
  • 26
  • 104
  • 151
Greg
  • 21,235
  • 17
  • 84
  • 107
30

I wrote a C# port of the Ruby Faker gem that can be used to easily generate fake data: names, addresses, phone numbers and the lorem ipsum text.

It's available as a NuGet package (Install-Package Faker.Net) with source on Github and I also wrote a post introducing some of it's features, with sample code.

Ben Smith
  • 2,369
  • 20
  • 17
27

Like this:

const string LoremIpsum = @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

To repeat it:

String.Join(Environment.NewLine, 
            Array.ConvertAll(new int[count], i => LoremIpsum));

Or, in .Net 4.0:

String.Join(Environment.NewLine, Enumerable.Repeat(LoremIpsum, count));
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
10

There's actually a package out on Nuget that does this exact thing for you.

http://www.nuget.org/packages/NLipsum/

For example, you can generate a paragraph of text by just doing this:

var someComments = new NLipsum.Core.Paragraph();
Joseph
  • 25,330
  • 8
  • 76
  • 125
2

Why not to use Lorem Ipsum Online generator?

I wrote this code that extracts the lorem ispum string from HTML page:

string LoremIpsum()
{
   string HTML = null;
   WebRequest request = WebRequest.Create("http://lipsum.com/feed/html"); 
   request.Credentials = CredentialCache.DefaultCredentials;
   HttpWebResponse response = (HttpWebResponse)request.GetResponse();
   Stream dataStream = response.GetResponseStream();
   StreamReader reader = new StreamReader(dataStream);
   HTML = reader.ReadToEnd(); //se citeste codul HTMl

   //searching for Lorem Ipsum
   HTML = HTML.Remove(0, HTML.IndexOf("<div id=\"lipsum\">")); 
   HTML = HTML.Remove(HTML.IndexOf("</div>"));
   HTML = HTML
        .Replace("<div id=\"lipsum\">", "")
        .Replace("</div>", "")
        .Replace("<p>", "")
        .Replace("</p>", "");

   reader.Close();
   dataStream.Close();
   response.Close();
   return HTML; 
}
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • 4
    Why rely on an external service (which could go down sooner or later) for this simple algo ? – A. M. Oct 10 '13 at 17:52
  • Pretty neat example, thank you for sharing. Totally fine for test scenarios - which frankly is about the only reason I want to use Lorem ipsum text in the first place - and can easily be adapted to other use cases beyond this Lorem ipsum page. – nicdaniau May 14 '20 at 09:42
2

Hello
you can use WordGenerator or LoremIpsumGenerator from MMLib.RapidPrototyping nuget package.

using MMLib.RapidPrototyping.Generators;
public void Example()
{
   WordGenerator generator = new WordGenerator();
   var randomWord = generator.Next();

   Console.WriteLine(randomWord);

   LoremIpsumGenerator loremIpsumGenerator = new LoremIpsumGenerator();
   var text = loremIpsumGenerator.Next(3,3);

   Console.WriteLine(text);
} 

Nuget site
Codeplex project site

Mino
  • 305
  • 2
  • 12
2

Version using StringBuilder and without HTML tags (using new line instead of paragraph mark):

    private static string LoremIpsum(int minWords, int maxWords, int minSentences, int maxSentences, int numLines)
    {
        var words = new[]{"lorem", "ipsum", "dolor", "sit", "amet", "consectetuer", "adipiscing", "elit", "sed", "diam", "nonummy", "nibh", "euismod", "tincidunt", "ut", "laoreet", "dolore", "magna", "aliquam", "erat"};

        var rand = new Random();
        int numSentences = rand.Next(maxSentences - minSentences)
            + minSentences + 1;
        int numWords = rand.Next(maxWords - minWords) + minWords + 1;

        var sb = new StringBuilder();
        for (int p = 0; p < numLines; p++)
        {
            for (int s = 0; s < numSentences; s++)
            {
                for (int w = 0; w < numWords; w++)
                {
                    if (w > 0) { sb.Append(" "); }
                    sb.Append(words[rand.Next(words.Length)]);
                }
                sb.Append(". ");
            }
            sb.AppendLine();
        }
        return sb.ToString();
    }
Tomino
  • 5,969
  • 6
  • 38
  • 50
2

Minor modification to Greg + Tomino's excellent method above to capitalize the first word of each sentence. I also removed the trailing newline and removed some "+ 1"s that gave one too many. Very handy for testing word wrap capabilities of user interfaces! Thanks to Tomino & Greg.

private static string LoremIpsum(int minWords, int maxWords, int minSentences, int maxSentences, int numLines)
{
    var words = new[]{"lorem", "ipsum", "dolor", "sit", "amet", "consectetuer", "adipiscing", "elit", "sed", "diam", "nonummy", "nibh", "euismod", "tincidunt", "ut", "laoreet", "dolore", "magna", "aliquam", "erat"};

    var rand = new Random();
    int numSentences = rand.Next(maxSentences - minSentences)
        + minSentences;
    int numWords = rand.Next(maxWords - minWords) + minWords;

    var sb = new StringBuilder();
    for (int p = 0; p < numLines; p++)
    {
        for (int s = 0; s < numSentences; s++)
        {
            for( int w = 0; w < numWords; w++ )
            {
                if( w > 0 ) { sb.Append( " " ); }
                string word = words[ rand.Next( words.Length ) ];
                if( w == 0 ) { word = word.Substring( 0, 1 ).Trim().ToUpper() + word.Substring( 1 ); }
                sb.Append( word );
            }
            sb.Append(". ");
        }
        if ( p < numLines-1 ) sb.AppendLine();
    }
    return sb.ToString();
}
kEnobus
  • 66
  • 5
0

Found this Lorem Ipsum generator: http://www.gutgames.com/post/Lorem-Ipsum-Generator-in-C.aspx

Muxa
  • 5,563
  • 6
  • 46
  • 56
0

There's one in NuGet called NetFx Ipsum Generator.

You can install it with

Install-Package netfx-IpsumGenerator

It's pretty minimal though, I'm currently looking for a better one, or a way to contribute.

John MacIntyre
  • 12,910
  • 13
  • 67
  • 106