By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,994 Members | 2,073 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,994 IT Pros & Developers. It's quick & easy.

C# loop for mail body generation

P: 3
I was wondering if anyone could possibly assist me please. I'm going to come out and say I am not a C# dev and have limited knowledge on the subject but I am starting to learn it.

I being developing a mail generation tool to pump dummy mail into an exchange environment for testing purposes. Everything works beautifully and the mail generation is grand but another requirement has come up where they want valid words (doesn't have to be legible, but searchable, thus valid English words).

The list of words is not my issue, previously I was using the ascii table to produce garbled text, so now I have stipulated the list ie:

List<string> dictionary = new List<string>();

Now what I cant seem to get right is populating the email body with a random selection of these words with random paragraphs and sentences in random lengths. The above list is just a short version for testing, i have 500 plus words that will be used.

I did look at using the loop of for or foreach but, I am failing miserably.

If anyone could assist me or point me in the right direction I would greatly appreciate it.
Feb 9 '09 #1
Share this Question
Share on Google+
3 Replies

Expert 5K+
P: 7,872
Do the sentances make sense or can they be like:
"Hello pencil angry perfect great"?
If so you can just use a random number generator.
Generate a random number for the number of words in a sentance
then do a for loop X amount of times, generating X amount of random numbers, these numbers should fall withen the range of your dictionary collection as indexes.
Does that work for you?
Feb 9 '09 #2

P: 3
That would work perfectly, making sense is no requirement at all.
Just that the data needs to be searchable, thus the requirement for valid words.

So yes that should work nicely.
Feb 9 '09 #3

Expert 100+
P: 229
You could use the Random class to generate a random index for your List of strings in a loop:

Expand|Select|Wrap|Line Numbers
  1. const int totalNumberOfWords = 100;
  2. int count = 0;
  4. StringBuilder sb = new StringBuilder();
  5. Random rnd = new Random();
  7. while (count < totalNumberOfWords)
  8. {
  9.     // get a random word index
  10.     int randomIndex = rnd.Next(dictionary.Count);
  12.     // append the word
  13.     sb.Append(dictionary[randomIndex]);
  15.     // append a space
  16.     sb.Append(" ");
  18.     // next word
  19.     count++;
  20. }
You can also use the Random class to create sentences and paragraphs every once in a while. If you presume that the random generator creates randomly distributed numbers, then you can check (for example) if a rnd.NextDouble() returns a number greater than 0.95 - this should happen with 5% probability - and if yes, add a new line (or a dot to the StringBuilder).

Spammers use Markov chain algorithms to generate pseudo-random texts, based on some input data, so if you need your text to have at least some structure, you might try to Google it.

EDIT: Ooops, sorry - I just saw previous posts.
Feb 9 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.