For sequential variations, I would do these:
- Put them in
Array
of words by split(' ')
- Generate a random value from 0 to length of
Array
minus 5 by Random
- Put them in a sentence, gives some spaces.
VB version + testing result
(This might be what you are more interested in)
Imports System
Imports System.Text
Public Module Module1
Public Sub Main()
Dim str As String = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
Console.WriteLine(GrabRandSequence(str))
Console.WriteLine(GrabRandSequence(str))
Console.WriteLine(GrabRandSequence(str))
Console.ReadKey()
End Sub
Public Function GrabRandSequence(inputstr As String)
Try
Dim words As String() = inputstr.Split(New Char() {" "c})
Dim index As Integer
index = CInt(Math.Floor((words.Length - 5) * Rnd()))
Return [String].Join(" ", words, index, 5)
Catch e As Exception
Return e.ToString()
End Try
End Function
End Module
Result
C# version
string[] words = input.Split(' '); //Read 1.
int val = (new Random()).Next(0, words.Length - 5); //Read 2.
string result = string.Join(" ", words, val, 5); //Read 3. improved by Enigmativy's suggestion
Additional try
For random variations, I would do these:
- Clean up all unnecessary characters (., etc)
- Put them in a
List
by LINQ
split(' ')
- Select
Distinct
among them by LINQ
(optional, to avoid result like Lorem Lorem Lorem Lorem Lorem
)
- Generate 5 distinct random values from 0 to size of
List
by Random
(repeat the picking when not distinct)
- Pick the words according to random values from the
List
- Put them in a sentence, gives some spaces.
Warning: the sentence may not make any sense at all!!
C# version (only)
string input = "the input sentence, blabla";
input = input.Replace(",","").Replace(".",""); //Read 1. add as many replace as you want
List<string> words = input.Split(' ').Distinct.ToList(); //Read 2. and 3.
Random rand = new Random();
List<int> vals = new List<int>();
do { //Read 4.
int val = rand.Next(0, words.Count);
if (!vals.Contains(val))
vals.Add(val);
} while (vals.Count < 5);
string result = "";
for (int i = 0; i < 5; ++i)
result += words[vals[i]] + (i == 4 ? "" : " "); //read 5. and 6.
Your result is in the result