I want to insert a random amount of dots (from 1 to 7) on random parts of a string without breaking the layout.
This is my current code:
Random rand = new Random();
string[] words = iTemplate.Text.Split(' ');
string result = string.Empty;
for (int i = 0; i < words.Count(); i++)
{
string word = words[i];
if (rand.Next(i, words.Count()) == i)
{
for (int dots = rand.Next(1, 7); dots > 0; dots--)
word += ".";
}
result += word + " ";
}
Is there a more efficient or nice LINQ option to it ?
Right now, since its random, there may be cases of no dots showing up. I have narrowed it by using if (rand.Next(i, words.Count()) == i)
which seems to work but still some results only show 1 to 3 places having dots inserted.
How could I guarantee that the dots are inserted a minimum of 4 different places during the process ?
Sample data/result as per comment request:
string template = "Hi, this is a template with several words on it and I want to place random dots on 4 different random places every time I run the function";
Result 1:
string result = "Hi, this... is a template with several.. words on it and. I want to place random dots on 4 different random...... places every time I run the function";
Result 2:
string result = "Hi, this is a template. with several... words on it and I want to..... place random dots on 4 different random. places every time I run the function";
Result 3:
string result = "Hi, this. is a template with... several words on it and I want to place random.. dots on 4 different random....... places every time I run the.. function";