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.
In VS Code if I type "lorem" and then press enter it will generate a paragraph of lorem ipsum.
The lorem ipsum is based on De finibus bonorum et malorum, a Latin text written by Cicero in 45 BC. Typographers and printers have used passages from this work for formatting since the 16th century. Many words have been added or modified over the centuries.
This free lorem ipsum generator lets you choose how many sentences, paragraphs or list items you want. You can also select to include HTML markup and specify how big the text should be.
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();
}
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.
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));
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;
}
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();
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
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();
}
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With